c# - Error with ViewModels and Create/Edit Actions -


i'm trying create asp.net mvc 2 webapp using northwind database following nerddinner tutorial, keep getting following error when trying edit product:

value of member 'supplierid' of object of type 'supplier' changed. member defining identity of object cannot changed. consider adding new object new identity , deleting existing 1 instead.

this happens when change category and/or suppliers (both dropdownlists), other fields (checkbox , textbox) ok.

i can't create new product since model.isvalid return false reason (no exceptions).

what doing wrong?

productcontroller.cs

    public actionresult edit(int id) {         product producttoedit = productsrepository.get(id);          return view(new productviewmodel(producttoedit));     }      [httppost]     public actionresult edit(int id, formcollection formvalues) {         product producttoedit = productsrepository.get(id);          if (tryupdatemodel(producttoedit, "product")) {             productsrepository.save();             return redirecttoaction("details", new { id = producttoedit.productid });         }          return view(producttoedit);     } 

productviewmodel.cs

public class productviewmodel {      public product product { get; private set; }     public selectlist suppliers { get; private set; }     public selectlist categories { get; private set; }      public productviewmodel(product product) {         this.product = product;          this.suppliers = new selectlist(new suppliersrepository()             .getallsuppliers()             .select(s => new selectlistitem {                 text = s.companyname,                 value = s.supplierid.tostring()             }), "value", "text");          this.categories = new selectlist(new categoriesrepository()             .getallcategories()             .select(c => new selectlistitem {                 text = c.categoryname,                 value = c.categoryid.tostring()             }), "value", "text");     } } 

productform.ascx

        <div class="editor-label">             <%= html.labelfor(model => model.product.supplierid) %>         </div>          <div class="editor-field">             <%= html.dropdownlistfor(model => model.product.supplier.supplierid, model.suppliers) %>         </div> 

of course, these codes excerpts of each controller , views. productviewmodel complete code. omited productrepository class.

put break point in ur controller action method , read modelstate object. inspect each key check if there error. description of error help. before try

<div class="editor-field">             <%= html.dropdownlistfor(model => model.supplierid, model.suppliers) %>         </div> 

this when editing foreign key value through listbox in l2s. not sure if u r using ef.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -