Trying to edit an entity with data from dropdowns in MVC
I'm having trouble getting my head around sending multiple models to a view in mvc. My problem is the following.
Using EF4 I have a table with attributes organised by category.
Couldn't post an image :-( [Have a table called attributes (AttributeTitle, AttributeName, CategoryID) connected to a table called Category (CategoryTitle).]
What I want to do is be able to edit an attribute entity and have a dropdown of categories to choose from.
I tried to make a custom viewmodel
public class AttributeViewModel
{
public AttributeViewModel()
{
}
public Attribute Attribute { get; set; }
public IQueryable<Category> AllCategories { get; set; }
}
But it just ended up being a mess.
<div class="editor-field">
<%: Html.DropDownList("Category", new SelectList((IEnumerable)Model.AllCategories, "CategoryID", "CategoryName")) %>
</div>
I was getting it back to the controller...
[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);
}
}
I've debugged the code and my _attribute looks correct and _attribute.CategoryID = (int)_selcategory updates th开发者_StackOverflowe model, but then I get the error.
Somewhere here I thought that there should be a cleaner way to do this, and that if I could only send two models to the view instead of having to make a custom viewmodel.
To sum it up: I want to edit my attribute and have a dropdown of all of the available categories.
Any help much appreciated!
Solved it!
If anyone is interested, my solution is the following. I created a class:
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; }
}
My controller:
// 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);
}
Part of my view:
<%@ 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>
And the postback:
// 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);
}
}
Basically my problem was due to my lack of understanding what updatemodel did. So after "_attribute.CategoryID = _selcategory" all I had to do was savechanges, but first I tried to updatemodel which kept failing.
Hope this helps someone else!
精彩评论