Binding DropDownList into MVC View

前端 未结 3 1957
悲&欢浪女
悲&欢浪女 2020-12-09 06:27

Reading from here: ASP.NET MVC

Action SelectCategory has been created inside controller -

 public ActionResult SelectCategory() {

              


        
3条回答
  •  长情又很酷
    2020-12-09 06:58

    A painless way for data binding to bind data to the model: Do not use the razor for Dropdown. Bind it manually, get it with FormCollection object.

    Controller:

     [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Sorun sorun,FormCollection fc)
        {
            //Define independently as form collection
    
            sorun.GonderilecekBirim = fc["GonderilecekBirim"];
            sorun.IlkSorulmaTarihi = DateTime.Now;
            if (ModelState.IsValid)
            {
    
                db.Soruns.Add(sorun);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            return View(sorun);
        }
    

    View

    @Html.LabelFor(model => model.GonderilecekBirim, htmlAttributes: new { @class = "control-label col-md-2" })
            
    @*@Html.ValidationMessageFor(model => model.GonderilecekBirim, "", new { @class = "text-danger" })*@

提交回复
热议问题