c# - Nested partials and templates; How to implement a conditional view or editor? -


i've been using c# while, , have done share of web-forms, i'm new mvc , don't know how phrase question googles.

i'm developing mvc site backed ef , trying hang of things. have view allows user create entity "a". entity has reference entity b, it's address, in turn has reference entity c, address' region.

so i've figured out can create editor template can injected create view entity a, , returns model it's entity b reference populated. can same thing on entity b template, injecting editor template entity c , getting correct entity in b's reference property.

my problem entity c created entity. user needs ability create one, , it's trivial entity (has name , country_enum), don't want user have visit special page create entity after they've figured out correct c entity isn't yet in list, come page , reenter other fields might have filled.

how can create "conditional" view or set of views, allow user either select existing entity c, or create new entity c if need to, without leaving create - entity page?

for clarity current setup:

entitya-create.cshtml

@using (html.beginform()) {   @html.antiforgerytoken()   @html.hiddenfor(a => a.parentid)   <fieldset>     //entity editors etc...     @html.editorfor(a => a.address, "address.cshtml", null)   </fieldset>       } 

editortemplates/address.cshtml

@model models.address <div class="editor-label">   @html.labelfor(b => b.property1) </div> <div class="editor-field">   //entity b property editors etc... </div> @html.editorfor(b=> b.region, "region.cshtml", null) 

in second view/template, need way render either full "create new region" editor template (which now), or "select existing region dropdown" view/template/etc, still return region model same way (or close to). preference avoid js as possible solution, you're experts!

you need create select list domain regions. personaly use view model such methods.

        public class address         {             public address()             {                 regions  = new list<selectlistitem>();             }             //your address properties             public int selectedregionid {get; set;}             public ienumerable<selectlistitem> regions {get; set;}             //you can fill regions list in controller, or in contructor             public ienumerable<selectlistitem> createregionsselectlist()             {                 var regions = new list<selectlistitem>();                 var domainregions = // regions repository;                  foreach (var region in domainregions)                     regions.add(new selectlistitem { text = region.name, value = region.id};             }         } 

then can create drop down domain regions in address editor:

@model models.address <div class="editor-label">   @html.labelfor(b => b.property1) </div> <div class="editor-field">   //entity b property editors etc... </div> select existing region: @html.dropdownfor(x => x.selectedregionid, model.regions, new {name = "address.selectedregionid"}) or create new region: <input id="createregion" type="button" value="create region" /> <div id="regionseditorwrapper" display:"style:none"> @html.editorfor(b=> b.region, "region.cshtml", null) </div> 

and show hidden editor simple jquery function.

$(document).ready(function(){     $("#createregion").click(function(){         $("#regionseditorwrapper").show();     }); }); 

if select drop-down , post, should region id in post action method. haven't tested this, should give direction. try it. luck.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -