简体   繁体   中英

Binding DropDownList with Model

The values "HerstellerId" and "BaureiheId" selected in the DropDownList in the Create.cshtml are not binded to the model. In HomeController in [HttpPost]Create the values "auswahl.HerstellerId" and "auswahl.BaureiheId" are 0 (Int32) and why is ModelState.IsValid true?

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>

}

我正在使用FormCollection public ActionResult Create(FormCollection collection) ,我可以使用int herstellerId = Convert.ToInt32(collection["Hersteller"]);获取我的值herstellerId = Convert.ToInt32(collection["Hersteller"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM