use*_*943 13 asp.net-mvc html.dropdownlistfor razor
我正在使用MVC.我想传递我从视图中输入的类别数据并传递给我的Post/Createcontroller,但这并不是让我传递我从下拉列表中选择的categoryTypeID.
这是错误:
DataBinding:'System.Web.Mvc.SelectListItem'不包含名为'CategoryTypeID'的属性.
这是我的代码:
My CreateController:
//
        // POST: /Category/Create
        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryTypes = new SelectList(db.CategoryTypes, "CategoryTypeID", "Name", category.CategoryTypeID);
            return View(category);
        }
My Create View
@model Haykal.Models.Category
<div class="editor-label">
            @Html.LabelFor(model => model.CategoryTypeID, "CategoryType")
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.CategoryTypeID,
            new SelectList(ViewBag.CategoryTypes as System.Collections.IEnumerable, "CategoryTypeID", "Name"),
          "--select Category Type --", new { id = "categoryType" })
            @Html.ValidationMessageFor(model => model.CategoryTypeID)
        </div>
Ani*_*esh 24
我遇到了这个错误.我绑定了View Model的一个对象:
editPanelViewModel.Panel = new SelectList(panels, "PanelId", "PanelName");
在View中,我创建了ListBox,如下所示:
@Html.ListBoxFor(m => m.Panel, new SelectList(Model.Panel, "PanelId", "PanelName"))
事实应该是这样的:
@Html.ListBoxFor(m => m.Panel, new SelectList(Model.Panel, "Value", "Text"))
Ε Г*_*И О 13
您SelectList在控制器和视图中定义了两次.
保持视图清洁.在您的情况下,以下就足够了:
@Html.DropDownListFor(model => model.CategoryTypeID, (SelectList)ViewBag.CategoryTypes)
我不得不承认DropDownListFor在开始时很混乱:)
| 归档时间: | 
 | 
| 查看次数: | 16107 次 | 
| 最近记录: |