asp.net mvc 4 - how to insert data in multiple tables at same time in mvc 4 razor using entity framework -


my controller

public actionresult create(product collection1, coin_bar collection2, jewellery collection3, gift collection4, stockdetail collection5, productrating collection6)         {             //var vm=new viewmodel();             //vm.coin_bars=entity.coin_bar();             //vm.         if (modelstate.isvalid)         {             var result1=entity.products.add(collection1);             entity.savechanges();             long productids = result1.productid;              collection2.productid = productids;             var result2 = entity.coin_bar.add(collection2);             entity.savechanges();              collection3.productid = productids;             var result3 = entity.jewelleries.add(collection3);             entity.savechanges();              collection4.productid = productids;             var result4 = entity.gifts.add(collection4);             entity.savechanges();              collection5.productid = productids;             var result5 = entity.stockdetails.add(collection5);             entity.savechanges();              collection6.productid = productids;             var result6 = entity.productratings.add(collection6);             entity.savechanges();              return redirecttoaction("index");         }         return view();     } 

how add view table fields in 1 view? please me..

an mvc view can work single model. result, can't post data multiple different entity types separate parameters you're trying in code provided.

however, can utilize view model encapsulate of these can work each individually while still passing 1 "model" , forth in view. example:

public class collectionviewmodel {     public collectionviewmodel()     {         product = new product();         coin_bar = new coin_bar();         jewellery = new jewellery();         stockdetail = new stockdetail();         productrating = new productrating();     }      public product product { get; set; }     public coin_bar coin_bar { get; set; }     public jewellery jewellery { get; set; }     public gift gift { get; set; }     public stockdetail stockdetail { get; set; }     public productrating productrating { get; set; } } 

new view model in action , pass view:

var model = new collectionviewmodel(); return view(model); 

then, in view, can create fields each like:

@model namespace.to.collectionviewmodel  ...  @html.editorfor(m => m.product.someproperty) 

and finally, in post action, receive view model:

public actionresult create(collectionviewmodel model) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

mysql - FireDac error 314 - but DLLs are in program directory -