asp.net - How to pre-select a drop down option list based on the parent key -
i have 2 entities rack , server. each server has foreign key parent rack. assigning server rack, using drop down list follow inside server create view. server create action method looks follow:-
public actionresult create() { populateviewbagdata(); return view(new serverjoin() { isipunique = true, ismacunique = true}); }
part of server create view include drop down list select rack follow:-
@model tms.viewmodels.serverjoin <div> <span class="f"> rack</span> @html.dropdownlistfor(model =>model.server.rackid, ((ienumerable<tms.models.tmsrack>)viewbag.racks).select(option => new selectlistitem { text = (option == null ? "none" : option.technology.tag), value = option.tmsrackid.tostring(), selected = (model.server != null) && (option.tmsrackid == model.server.rackid) }), "choose...") @html.validationmessagefor(model =>model.server.rackid) </div>
what trying implement , inside rack view want add link add server, , force rack drop down list select current rack , such as:-
@html.actionlink(“create server under rack”, “create”,”server”, new {rackid = model.rackid},null)
but not sure how force drop down list select rackid passed, baring in mind user can still create server without going rack, rackid null??
any idea how implement ? br
here how should it:
public actionresult create(int? rackid) { var model = new serverjoin() { isipunique = true, ismacunique = true}; if(rackid.hasvalue) { model.rackid = rackid.value; } populateviewbagdata(); return view(model); }
then, in view, can use if clause replace dropdownlist hidden input if model has rackid.
the actionlink
in rack view correct.
Comments
Post a Comment