我有一个问题,我需要将Viewdata数据转换为View
中的模型对象模型:
public class ProductModel
{
public ProductModel()
{
Category = new List<SelectListItem>();
Category.Add(new SelectListItem
{
Text = "Books",
Value = "1"
});
Category.Add(new SelectListItem
{
Text = "Mobiles & Tablets",
Value = "2"
});
Category.Add(new SelectListItem
{
Text = "Laptops & Accessories",
Value = "3"
});
}
public int ProductId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public string Manufacturer { get; set; }
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
public decimal BasePrice { get; set; }
public IList<SelectListItem> Category { get; set; }
public int CategoryId { get; set; }
}
控制器:
public ActionResult ProductHtml()
{
ProductModel model = new ProductModel();
Viewdata["VD"]=model;
return View(model);
}
这样我就可以绑定下面的值了。
那么如何将ViewData
值转换为此处的模型对象并绑定
@Html.DropDownListFor(model => model.CategoryId, Model.Category, "--Select--")
@Html.TextBoxFor(model => model.Description)
这可以在MVC中实现吗?
答案 0 :(得分:0)
就这样做,
public ActionResult ProductHtml()
{
ProductModel model = new ProductModel();
return View(model);
}
查看强>
@Html.DropDownListFor(model => model.CategoryId, Model.Category, "--Select--")