0
  C# code 
 public class customer
   {
    [Required]
    [Display(Name = "Country")]
    public int CountryId { get; set; } 
   }

ViewBag.CountryId = new SelectList(Uow.Countries.GetAll(), "ID", "名前");

  modelbuilder 
       modelBuilder.Entity<Customer>().HasRequired(p => p.Country)
                                      .WithMany(p => p.Customers)
                                      .HasForeignKey(p => p.CountryId)
                                      .WillCascadeOnDelete(false);




      <div class="control-group">
        @Html.LabelFor(model => model.CountryId, "CountryId", new { @class = "control-    label" })
          <div class="controls">
         @Html.DropDownList("CountryId","select country"); 
         @Html.ValidationMessageFor(model => model.CountryId, null, new { @class =  "help-inline" })
        </div>
        </div>

デフォルト値が「国を選択」の保存ボタンをクリックしても、モデル エラーが発生しません」 代わりにエラーが発生します」 "

4

1 に答える 1

1

CountryIdあなたのドロップダウンリストではModel.CountryId、そうではありませんViewBag.CountryId。これを試して :

@Html.DropDownListFor(model => model.CountryId, (SelectList)ViewBag.CountryId,"select country"); 

また、コレクションと選択された値を取得するプロパティに別の名前を選択することをお勧めします。

于 2013-10-25T09:52:45.150 に答える