我无法将多个模型发送到mvc中的视图。 我的问题是以下。尝试使用MVC中的下拉列表中的数据编辑实体
使用EF4我有一个按类别组织的属性表。
无法张贴:-( 图像[已连接到一个表名为类别(CategoryTitle)的表称为属性(AttributeTitle,为AttributeName,类别ID)
我想要做的是能编辑属性的实体,具有类别的下拉菜单选择。
我试图使自定义视图模型
public class AttributeViewModel
{
public AttributeViewModel()
{
}
public Attribute Attribute { get; set; }
public IQueryable<Category> AllCategories { get; set; }
}
,但它只是结束了一个烂摊子。
<div class="editor-field">
<%: Html.DropDownList("Category", new SelectList((IEnumerable)Model.AllCategories, "CategoryID", "CategoryName")) %>
</div>
我得到它回到控制器...
[HttpPost]
public ActionResult Edit(int AttributeID, FormCollection formcollection)
{
var _attribute = ProfileDB.GetAttribute(AttributeID);
int _selcategory = Convert.ToInt32(formcollection["Category"]);
_attribute.CategoryID = (int)_selcategory;
try
{
UpdateModel(_attribute); (<---Error here)
ProfileDB.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception e)
{
return View(_attribute);
}
}
我调试的代码和我_attribute看起来是正确的和_attribute.CategoryID =(INT)_selcategory更新模型,但随后我收到错误。
在某处,我认为应该有一个更干净的方式来做到这一点,如果我只能发送两个模型到视图,而不是必须制作自定义视图模型。
总结: 我想编辑我的属性,并有所有可用类别的下拉列表。
任何帮助非常感谢!
错误信息会有帮助。 – jfar 2011-02-23 16:02:55
对不起,直到现在还没有看到您的评论。它说类似“不能更新类型...属性的模式”。原因是我试图更新模型。 – bek 2011-02-24 14:20:56