Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to edit an entity with data from dropdowns in MVC
    primarykey
    data
    text
    <p>I'm having trouble getting my head around sending multiple models to a view in mvc. My problem is the following.</p> <p>Using EF4 I have a table with attributes organised by category.</p> <p>Couldn't post an image :-( [Have a table called attributes (AttributeTitle, AttributeName, CategoryID) connected to a table called Category (CategoryTitle).]</p> <p>What I want to do is be able to edit an attribute entity and have a dropdown of categories to choose from.</p> <p>I tried to make a custom viewmodel</p> <pre><code> public class AttributeViewModel { public AttributeViewModel() { } public Attribute Attribute { get; set; } public IQueryable&lt;Category&gt; AllCategories { get; set; } } </code></pre> <p>But it just ended up being a mess.</p> <pre><code>&lt;div class="editor-field"&gt; &lt;%: Html.DropDownList("Category", new SelectList((IEnumerable)Model.AllCategories, "CategoryID", "CategoryName")) %&gt; &lt;/div&gt; </code></pre> <p>I was getting it back to the controller... </p> <pre><code> [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); (&lt;---Error here) ProfileDB.SaveChanges(); return RedirectToAction("Index"); } catch (Exception e) { return View(_attribute); } } </code></pre> <p>I've debugged the code and my _attribute looks correct and _attribute.CategoryID = (int)_selcategory updates the model, but then I get the error. </p> <p>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.</p> <p>To sum it up: I want to edit my attribute and have a dropdown of all of the available categories.</p> <p>Any help much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload