c# - MVC 4 - Centralize the create action -


i'll try clear. i'm developing web application based on mvc 4 , entity framework. through app, can create products depending on other table product types can see, in create product view, have dropdownlist contains product types :

@model busimaterial.models.product @{     viewbag.title = "create"; } <h2>     create</h2> @using (html.beginform()) {     @html.validationsummary(true)      <fieldset>         <legend>product</legend>         <div class="editor-label">             purchase date :         </div>         <div class="editor-field">             @html.textboxfor(model => model.purchasedate, new { @class = "datepicker"})             @html.validationmessagefor(model => model.purchasedate)         </div>         <div class="editor-label">             serial number :         </div>         <div class="editor-field">             @html.textboxfor(model => model.serialnumber, new { maxlength = 50 })             @html.validationmessagefor(model => model.serialnumber)         </div>         <div class="editor-label">             product type :         </div>         <div class="editor-field">             @html.dropdownlist("id_producttype", string.empty)<a href="../producttype/create">add                 new product type?</a>             @html.validationmessagefor(model => model.id_producttype)         </div>         <div class="form-actions">           <button type="submit" class="btn btn-primary">create</button>         </div>     </fieldset> } 

and in create product type view, have dropdownlist of existing product companies (so same relation exists between product , product type :

@model busimaterial.models.producttype  @{     viewbag.title = "create"; }  <h2>create</h2>  @using (html.beginform()) {     @html.validationsummary(true)      <fieldset>         <legend>producttype</legend>          <div class="editor-label">             model :          </div>         <div class="editor-field">             @html.textboxfor(model => model.model, new { maxlength = 50 })             @html.validationmessagefor(model => model.model)         </div>          <div class="editor-label">             catalog price :          </div>         <div class="editor-field">             @html.editorfor(model => model.catalogprice)             @html.validationmessagefor(model => model.catalogprice)         </div>          <div class="editor-label">             company :          </div>         <div class="editor-field">             @html.dropdownlist("id_productcompany", string.empty)<a href ="../productcompany/create" >add new company?</a>             @html.validationmessagefor(model => model.id_productcompany)         </div>          <div class="form-actions">           <button type="submit" class="btn btn-primary">create</button>         </div>     </fieldset> } 

what tried "mix" these 2 views in 1 create product view. think action change little bit. also, think i'll have 2 adds in database. best way want?

update : using viewmodel, got :

my view model :

public class productviewmodel {     [required]     public product product { get; set; }     [required]     public producttype producttype { get; set; } } 

my create action :

[httppost] public actionresult createfullproduct(productviewmodel pvm) {     viewbag.id_productcompany = new selectlist(db.productcompanies, "id_productcompany", "name", pvm.producttype.id_productcompany);      if (modelstate.isvalid)     {         product product = new product { purchasedate = pvm.product.purchasedate,                                          serialnumber = pvm.product.serialnumber,                                         id_producttype = pvm.producttype.id_producttype};          producttype producttype = new producttype {model = pvm.producttype.model,                                                    catalogprice = pvm.producttype.catalogprice,                                                    id_productcompany = pvm.producttype.id_productcompany};          db.producttypes.addobject(producttype);         db.products.addobject(product);         db.savechanges();         return redirecttoaction("index", "person");             }      return view(pvm); } 

when try save new entries, got issue : the insert statement conflicted foreign key constraint "fk_bm_producttypes_bm_productcompanies".

as products "dependent" on producttypes, merging them 1 idea. you'll have merge post action too, have 2 inserts database (which correct, 1 product, 1 producttype.

you'll have put them both in 1 model can use in view, like:

public class productviewmodel {     public product product { get; set; }     public producttype producttype { get; set; } } 

edit: problem saving because productcomany not being posted (as indiciated in chat)

to fix that, first we'll put values dropdown in model:

public class productviewmodel {     public product product { get; set; }     public producttype producttype { get; set; }     public list<selectlistitem> productcompanies { get; set; } } 

then populate in httpget , httppost doing:

model.productcompanies = db.productcompanies     .tolist()     .select(s => new selectlistitem     {         text = s.name,         value = s.id.tostring()     })     .tolist(); 

then in view can do:

@html.dropdownlistfor(m => m.producttype.id_productcompany, model.productcompanies) 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -