2011-02-23 14 views
0

我无法将多个模型发送到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更新模型,但随后我收到错误。

在某处,我认为应该有一个更干净的方式来做到这一点,如果我只能发送两个模型到视图,而不是必须制作自定义视图模型。

总结: 我想编辑我的属性,并有所有可用类别的下拉列表。

任何帮助非常感谢!

+0

错误信息会有帮助。 – jfar 2011-02-23 16:02:55

+0

对不起,直到现在还没有看到您的评论。它说类似“不能更新类型...属性的模式”。原因是我试图更新模型。 – bek 2011-02-24 14:20:56

回答

0

解决了!

如果有人有兴趣,我的解决方案如下。 我创建了一个类:

public class AttributeEditViewModel 
{ 
    public AttributeEditViewModel() 
    { 
    } 

    public AttributeEditViewModel(Attribute attribute, SelectList categories) 
    { 
     this.attribute = attribute; 
     this.categories = categories; 
    } 

    public Attribute attribute { get; set; } 
    public SelectList categories { get; set; } 
} 

我的控制器:

 // GET: /Attribute/Edit/5 
    public ActionResult Edit(int AttributeID) 
    { 
     Attribute _attribute = ProfileDB.GetAttribute(AttributeID); 

     var _viewmodel = new AttributeEditViewModel 
     { 
      attribute = _attribute, 
      categories = new SelectList(ProfileDB.GetAllCategories(), "CategoryID", "CategoryName", _attribute.CategoryID) 
     }; 

     return View(_viewmodel); 
    } 

我的部分观点:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<KnowledgePortal.ViewModel.AttributeEditViewModel>" %> 



<div class="editor-field"> 
    <%: Html.DropDownList("Category", Model.categories, Model.attribute.CategoryID) %> 
</div> 

<p> 
    <input type="submit" value="Save" /> 
</p> 

而且回传:

// POST: /Attribute/Edit/5 
[HttpPost] 
public ActionResult Edit(int AttributeID, FormCollection formcollection) 
{ 
    var _attribute = ProfileDB.GetAttribute(AttributeID); 
    int _selcategory = Convert.ToInt32(formcollection["Category"]); 
    _attribute.CategoryID = (int)_selcategory; 

    try 
    { 
     ProfileDB.SaveChanges(); 

     return RedirectToAction("Index"); 
    } 
    catch (Exception e) 
    { 

     return View(_attribute); 
    } 
} 

基本上我的问题是由于我的缺乏对updatemodel做什么的理解。 因此,在“_attribute.CategoryID = _selcategory”之后,我所要做的就是保存更改,但是首先尝试更新模型,并保持失败状态。

希望这可以帮助别人!