asp.net mvc - Trying to edit an entity with data from dropdowns in MVC -


i'm having trouble getting head around sending multiple models view in mvc. problem following.

using ef4 have table attributes organised category.

couldn't post image :-( [have table called attributes (attributetitle, attributename, categoryid) connected table called category (categorytitle).]

what want able edit attribute entity , have dropdown of categories choose from.

i tried make custom viewmodel

    public class attributeviewmodel {     public attributeviewmodel()     {     }      public attribute attribute { get; set; }     public iqueryable<category> allcategories { get; set; } } 

but ended being mess.

<div class="editor-field">         <%: html.dropdownlist("category", new selectlist((ienumerable)model.allcategories, "categoryid", "categoryname")) %>     </div> 

i getting 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 code , _attribute looks correct , _attribute.categoryid = (int)_selcategory updates model, error.

somewhere here thought there should cleaner way this, , if send 2 models view instead of having make custom viewmodel.

to sum up: want edit attribute , have dropdown of of available categories.

any appreciated!

solved it!

if interested, solution following. created 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 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 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 problem due lack of understanding updatemodel did. after "_attribute.categoryid = _selcategory" had savechanges, first tried updatemodel kept failing.

hope helps else!


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -