2012-02-11 12 views
0

MVC 3の作成ページにドロップダウンリストがありますが、私はそのページをPOSTするときに値を取得しません。次のように私のコードは: -MVC 3ドロップダウンリストに値がありません

ビュー: -

@using (Html.BeginForm("Create", "League", FormMethod.Post, new { enctype = "multipart/form-data" })) 

{@ Html.ValidationSummary(真) リーグ

<div class="editor-label"> 
     @Html.LabelFor(model => model.League.fk_CountryID, "Country") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("fk_CountryID", "--Select One--")            *    
     @Html.ValidationMessageFor(model => model.League.fk_CountryID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.League.LeagueName) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.League.LeagueName) 
     @Html.ValidationMessageFor(model => model.League.LeagueName) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.League.Image) 
    </div> 
    <div class="editor-field"> 
     Upload File: <input type="file" name="Image" /> 
    </div> 


    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

}

コントローラー: -

 [HttpPost] 
    public ActionResult Create([Bind(Exclude = "Image")]Country country, HttpPostedFileBase Image, League league) 
    { 
     if (ModelState.IsValid) 
     { 

      model.League = league; 

      try 
      { 
       foreach (string file in Request.Files) 
       { 
        HttpPostedFileBase fileUploaded = Request.Files[file]; 

        if (fileUploaded.ContentLength > 0) 
        { 
         byte[] imageSize = new byte[fileUploaded.ContentLength]; 
         fileUploaded.InputStream.Read(imageSize, 0, (int)fileUploaded.ContentLength); 
         model.League.Image = imageSize; 
        } 
       } 

       db.Leagues.AddObject(model.League); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      catch (Exception e) 
      { 
       ModelState.AddModelError("uploadError", e); 
      } 
     } 

私は間違って何をしていますか?コントローラーのドロップダウンリストの値を取得できません。次のようにあなたの助けと時間を

おかげ

+0

をお使いのモデルがどのように見えるんどのように?なぜあなたのPOSTコントローラのアクションは非常に多くの引数を取るのですか?なぜビューモデルを使用していないのですか?モデルのどのコレクションプロパティにドロップダウンリストがバインドされていますか? –

+0

私のモデルはEFモデルです。カントリーにはCountryID、CountryNameおよびImageがあります。リーグにはリーグID、fk_CountryID、リーグ名、イメージがあります。私のコントローラは画像、国、リーグを獲得しています。私はViewModelsを使用しています、model.League = league; 。私のドロップダウンリストはCountryモデルにバインドされています。 – Johann

+0

あなたはビューモデルが何であるかについていくつかの誤解があるかもしれないと思います。ビューモデルには、ドメイン固有のオブジェクトが含まれていたり、参照されたりしてはなりませんあなたの 'Country'と' League'クラスはドメインモデルです。彼らは見解の中で何もしていません。ビューモデルは、ビュー用に特別に設計されたクラスです。私のViewModelに –

答えて

1

は、[OK]を、私のViewModelにSelectListのを追加することによって、それを修正: -

 //Countries dropdown List 
    public SelectList CountryList { get; set; } 
    public string SelectedCountry { get; set; } 

     public LeagueData PopulateCountriesDDL(string selected, Country country) 
    { 
     var typeList = new SelectList(db.Countries.ToList(), "CountryID", "CountryName", selected); 
     LeagueData model = new LeagueData { CountryList = typeList, Country = country, SelectedCountry = selected }; 

     return model; 
    } 
+0

私のDDLチュートリアルを見るhttp://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvcとhttp ://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx – RickAndMSFT

関連する問題