请给我一些答案!我总是有这个烦恼!这是我的控制器:
public ActionResult Create()
{
IEnumerable<SelectListItem> items =
_categoryService.GetAllCategory().Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
});
ViewBag.Categories = items;
return View();
}
[HttpPost]
public ActionResult Create(ProductViewModel productViewModel,Guid categoryId)
{
if (ModelState.IsValid)
{
_productService.Create(productViewModel);
}
return View();
}
这是我的观点:
@model Statos.Service.Products.ProductViewModel
@{
ViewBag.Title = "Create";
}
<fieldset>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(product => product.Name)
@Html.TextBoxFor(Product => Product.Name)
</div>
<div>
@Html.LabelFor(product => product.Brand)
@Html.TextBoxFor(product => product.Brand)
</div>
<div>
@Html.LabelFor(product => product.Category)
@Html.DropDownList("categoryId", (IEnumerable<SelectListItem>)ViewBag.Categories)
</div>
<div>
@Html.LabelFor(product => product.Price)
@Html.TextBoxFor(product => product.Price)
</div>
<div>
@Html.LabelFor(product => product.Description)
@Html.TextBoxFor(product => product.Description)
</div>
<div>
<input type="submit" value="Add" />
</div>
}
</fieldset>
现在如何将类别绑定到发布操作,在提交页面后出现此错误:“没有具有键 'categoryId' 的 'IEnumerable' 类型的 ViewData 项目”。所以我该怎么做 ?