2017-01-12 139 views
0

在Create.cshtml的DropDownList中選擇的值「HerstellerId」和「BaureiheId」不綁定到模型。在[HttpPost]中的HomeController中創建值「auswahl.HerstellerId」和「auswahl.BaureiheId」爲0(Int32),爲什麼ModelState.IsValid是true?綁定DropDownList與模型

HomeController.cs

public class HomeController : Controller 
    { 
    SclDataEntities sclDataEntities = new SclDataEntities(); 

    // GET: Home 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    // GET: /Home/Create 
    [HttpGet] 
    public ActionResult Create() 
    { 
     ViewBag.StateList = sclDataEntities.Hersteller; 

     Auswahl auswahl = new Auswahl(); 

     return View(auswahl); 
    } 

    // POST: /Home/Create 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create([Bind(Include = "HerstellerId, BaureiheId")] Auswahl auswahl) 
    { 
     ViewBag.StateList = sclDataEntities.Hersteller; 

     if (ModelState.IsValid) 
     { 
      sclDataEntities.Auswahl.Add(auswahl); 
      sclDataEntities.SaveChanges(); 

      return RedirectToAction("Details", new { id = auswahl.ID }); 
     } 
     else 
     { 
      return View(auswahl); 
     } 
    } 

    public JsonResult FillCity(int hersteller) 
    { 

     return Json(_FillCity(hersteller), JsonRequestBehavior.AllowGet); 
    } 

    public SelectList _FillCity(int hersteller) 
    { 
     IEnumerable<SelectListItem> cityList = new List<SelectListItem>(); 

     cityList = (from m in sclDataEntities.Baureihe where m.HerstellerId == hersteller select m) 
      .AsEnumerable().Select(m => new SelectListItem() 
      { 
       Text = m.Symbol, 
       Value = m.ID.ToString() 
      }); 

     return new SelectList(cityList, "Value", "Text"); 
    } 

Create.cshtml

@model Core.Auswahl 

@{ 
ViewBag.Title = "Create"; 
} 


@section scripts { 
<script> 
function FillCity() { 
    var herstellerId = $('#Hersteller').val(); 
    $.ajax({ 
     url: '/Home/FillCity', 
     type: "GET", 
     dataType: "JSON", 
     data: { hersteller: herstellerId }, 
     success: function (baureihen) { 
      $("#Baureihe").html(""); 
      $.each(baureihen, function (i, baureihe) { 
       $("#Baureihe").append(
       $('<option></option>').val(baureihe.Value).html(baureihe.Text)); 
      }); 
     } 
    }); 
} 
</script> 
} 

<h2>Auswahl</h2> 

@using (Html.BeginForm()) 
{ 
@Html.AntiForgeryToken() 

<div class="form-horizontal"> 

    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

    <div class="form-group"> 
     @Html.LabelFor(m => m.Hersteller, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(m => m.Hersteller, 
       new SelectList(ViewBag.StateList, "ID", "Symbol"), 
       "", new { @class = "form-control", onchange = "FillCity()" }) 
      @Html.ValidationMessageFor(m => m.HerstellerId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(m => m.Baureihe, new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(m => m.Baureihe, 
       new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), 
       null, new { @class = "form-control" }) 
      @Html.ValidationMessageFor(m => m.BaureiheId, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      @Html.ActionLink("abbrchen", "Index", null, new { @class = "btn btn-default" }) 
      <button class="btn btn-primary" type="submit">speichern</button> 
     </div> 
    </div> 

</div> 

}

+0

你申請任何註釋,例如[需要]給你的模型? –

+0

什麼具體不工作? – Shyju

+0

在我注意到的事情是,該模型有屬性Hersteller和Baureihe。但綁定是爲「HerstellerId」和「BaureiheId」完成的。不知道這是故意的,但這可能是問題的原因。 –

回答

0

我使用的FormCollection public ActionResult Create(FormCollection collection),我可以讓我的價值觀與詮釋herstellerId = Convert.ToInt32(collection["Hersteller"]);