c# - Inserting data into SQL database using EF -
i'm trying create httppost method create new database entry. should take 2 foreign ids different database tables , "name". here's model:
public class domena { public int domenaid { get; set; } // domains id public int tldid { get; set; } // foreign id public int klientid { get; set; } // foreign id public string nazwa { get; set; } public virtual tld tld { get; set; } public virtual klient klient { get; set; } }
right, have :
// get: /domena/add_domain public actionresult add_domain() { viewbag.tldid = new selectlist(db.tlds, "tldid", "typ"); viewbag.klientid = new selectlist(db.klienci, "klientid", "klientid"); return view(); } // // post: /domena/add_domain [httppost] [validateantiforgerytoken] public actionresult add_domain(domena domena) { if (modelstate.isvalid) { db.domeny.add(domena); db.savechanges(); return redirecttoaction("index"); } viewbag.tldid = new selectlist(db.tlds, "tldid", "typ", domena.tldid); viewbag.klientid = new selectlist(db.klienci, "klientid", "klientid", domena.klientid); return view(domena); }
the way works is, display drop-down list can choose tldid "typ" , klientid "klientid" entry in database. ask "nazwa", name has written.
i want remove option choose klientid dropdownlist , instead make httppost take klientid link. example :
- i go client's details page :
/klient/details/6
- i click on add_domain link takes viewed klientid , takes me to:
/domena/add_domain/6
so, question is, how can modify both , post methods in order create new "domena" entry in database client's id in link ?
do have change in view ?
here current add_domain view fieldset :
<fieldset> <legend>domena</legend> <div class="editor-label"> @html.labelfor(model => model.tldid) </div> <div class="editor-field"> @html.dropdownlist("tldid", string.empty) @html.validationmessagefor(model => model.tldid) </div> <div class="editor-label"> @html.labelfor(model => model.klientid) </div> <div class="editor-field"> @html.dropdownlist("klientid", string.empty) @html.validationmessagefor(model => model.klientid) </div> <div class="editor-label"> @html.labelfor(model => model.nazwa) </div> <div class="editor-field"> @html.editorfor(model => model.nazwa) @html.validationmessagefor(model => model.nazwa) </div> <p> <input type="submit" value="create" /> </p> </fieldset>
thanks in advance!
if use default mvc routes {controller}/{action}/{id}
should able leverage id
parameter. add_domain
link in client page set route value:
@html.actionlink("add domain", "add_domain", "domena", new { id = model.klientid }, null)
you give action parameter , set parameter on model:
public actionresult add_domain(int id) { viewbag.tldid = new selectlist(db.tlds, "tldid", "typ"); var model = new domena { klientid = id }; return view(model); }
and in view remove label , validation message klientid
, replace drop down list hidden input:
@html.hiddenfor(model => model.klientid)
in post action remove viewbag.klientid = ...
line. rest can remain unchanged. mvc model binder bind hidden field domena.klientid
property.
the id
parameter bit misused here because id
related model domena
controller dealing with, domenaid
, not klientid
. should still work. consider use query parameter instead make clearer last parameter in route klientid
:
the action link be:
@html.actionlink("add domain", "add_domain", "domena", new { klientid = model.klientid }, null)
and action is:
public actionresult add_domain(int klientid) { viewbag.tldid = new selectlist(db.tlds, "tldid", "typ"); var model = new domena { klientid = klientid }; return view(model); }
post action same. created link /domena/add_domain?klientid=6
.
Comments
Post a Comment