使用Model绑定DropDownList

时间:2017-01-12 15:56:52

标签: jquery ajax asp.net-mvc

Create.cshtml中DropDownList中选择的值“HerstellerId”和“BaureiheId”未绑定到模型。在[HttpPost]的HomeController中创建值“auswahl.HerstellerId”和“auswahl.BaureiheId”为0(Int32),为什么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>

}

1 个答案:

答案 0 :(得分:0)

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