asp.net mvc - Retrieving values from partial view during post method -


i have view contains dropdown list , on dropdownlist item being selected load partial view. , when form submitted want able both values main view , partial view during form submit. here main view

@model adminportal.areas.hardware.models.createmodule @{     viewbag.title = "create module";     layout = "~/views/shared/_bootstraplayout.basic.cshtml"; }  @html.validationsummary(true)   <fieldset class="form-horizontal">     <legend>add module <small>create</small></legend>     @using (html.beginform("createmodule", "module", new{id="addmoduleform"}))     {         @html.validationsummary(true)         <div class ="controls">             <div class="input-block-level">@html.textboxfor(model => model.moduleid, new {@placeholder = "moduleid"})</div>             <br/>             <div class ="input-block-level" id="selectedmoduletypename">@html.dropdownlistfor(model => model.selectedmoduletypename, model.typenames,"select moduletype", new{id = "modulelist"})</div>              <br/>             <div id="partialdiv"></div>         </div>          <div class="form-actions" id="buttons">         <button type="submit" class="btn btn-primary" id="submit">save changes</button>         @html.actionlink("cancel", "modulelist", null, new { @class = "btn " })     </div>      }  </fieldset> <div>     @html.actionlink("back list", "modulelist") </div> <script>     $("#buttons").hide();     $("#modulelist").on("change", function() {         var modid = $(this).val();         $.get('@url.action("getmodulepropertyname", "module")', { moduletypevalue: modid }, function(result) {             $("#partialdiv").html(result);         });         //uncomment following section check if partial view working         /*.done(function() { alert("done"); })             .fail(function() { alert("fail"); })             .always(function() { alert("completed"); });*/      });         $("#buttons").show();  </script> 

and here partial view

    @model ienumerable<string>      @foreach(var names in model)     {         <div class="input-block-level">@html.textboxfor(m=>names, new{value="", placeholder=names})</div>         <br/>     } 

here model

public class createmodule     {         //empty form handle form serialization         public createmodule()         {         }          [required]         public string moduleid { get; set; }          [datatype(datatype.datetime)]         public datetime dateentered { get; set; }          [required]         public string selectedmoduletypename { get; set; }         public ienumerable<selectlistitem> typenames { get; set; }          public list<property> properties { get; set; }     }  public class property     {         public string name { get; set; }         public string value { get; set; }     } 

here method script in main view forwards to

[httpget]         public actionresult getmodulepropertyname(string moduletypevalue)         {             var modulekindid = _repository.getmodulekindid(moduletypevalue);             var modulepropertynames = _repository.getmodulekindpropertynames(moduletypevalue);             return partialview("getmodulepropertyname",modulepropertynames);         } 

and here httppost method main view

[httppost]         public actionresult createmodule(createmodule modulev)         {             var module = new module                 {                     moduletypeid = convert.toint64(modulev.selectedmoduletypename),                     moduleid = modulev.moduleid,                     dateentered = modulev.dateentered,                   };             if (modelstate.isvalid)             {                _repository.addmodule(module);                 success("module added successfully!");                 return redirecttoaction("modulelist", "module", new {area = "hardware"});             }                   error("something went wrong!");                 return redirecttoaction("createmodule", "module", new { area = "hardware" });          } 

current situation: when form posted, properties value of model being passed via partial view null. other values, typename, module id.

what i'd want: want value of properties being passed via partial view.

you don't have input field properties property anywhere in form. null. that's normal.

here's how proceed. start setting correct navigational property helper generates correct names of corresponding input fields.

also make sure passing ienumerable<property> model partial if want able them correctly:

[httpget] public actionresult getmodulepropertyname(string moduletypevalue) {     var modulekindid = _repository.getmodulekindid(moduletypevalue);     ilist<property> model = ...     return partialview("getmodulepropertyname", model.tolist()); } 

and in partial view use editor template:

@model ilist<property> @{     // indicates current navigational context helpers     viewdata.templateinfo.htmlfieldprefix = "properties"; }  @html.editorformodel() 

and last step define custom editor template property class: ~/views/shared/editortemplates/property.cshtml (note name , location of template important)

@model property <div class="input-block-level">     @html.hiddenfor(m => m.name)     @html.textboxfor(m => m.value, new { placeholder = model.name }) </div> <br /> 

Comments