c# - MVC, Saving Data into 2 or more Related models from a single Form / View .? -
my model: (i) transaction
public class transaction { [key] public int transactionid { get; set; } public datetime onwarddate { get; set; } public datetime returndate { get; set; } public virtual userprofile user { get; set; } }
(ii) user
public class userprofile { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int userid { get; set; } public string username { get; set; } private userdetails _details = new userdetails(); public virtual userdetails details { { return _details; } } }
(ii) userdetails
public class userdetails { [key] public int detailsid { get; set; } public string dlnum { get; set; } }
my controller create transaction:
// get: /transaction/create public actionresult create() { return view(); } // // post: /transaction/create [httppost] [validateantiforgerytoken] public actionresult create(transaction transaction) { if (modelstate.isvalid) { db.transactions.add(transaction); db.savechanges(); return redirecttoaction("index"); } return view(transaction); }
my view create transaction:
<div class="editor-label"> @html.labelfor(model => model.onwarddate) </div> <div class="editor-field"> @html.editorfor(model => model.onwarddate) @html.validationmessagefor(model => model.onwarddate) </div> <div class="editor-label"> @html.labelfor(model => model.user.details.dlnum) </div> <div class="editor-field"> @html.editorfor(model => model.user.details.dlnum) @html.validationmessagefor(model => model.user.details.dlnum) </div>
when submit form... model.onwarddate getting saved model.user.details.dlnum not saving
hv seen viewmodel. option go it? in advance.
in case, think it's because setter private userdetails:
public virtual userdetails details { { return _details; } }
if you're creating tables based on models , therefore can't afford modify them, indeed advise make view model, post using , create / update database entry accordingly.
Comments
Post a Comment