c# - Passing viewmodel between partial views MVC5 -


i have main view , 2 partial views. need able populate viewmodel values in first partial view , pass viewmodel second partial view on click of button. button in second partial view. have written javascript function viewmodel empty when check controller method. you can see in screenshot below services box second partial view

first partial view

enter image description here

second partial view

@model cc.grp.mcrequest.viewmodels.newrequestviewmodel  <div id="newrequest">     <h1>services</h1>      @using (html.beginform())     {          @html.labelfor(model => model.projectname, htmlattributes: new { @class = "control-label col-md-5" })         @html.editorfor(model => model.projectname, new { htmlattributes = new { @class = "form-control", style = "width:100%" } })          <input type="submit" value="save" class="btn btn-default" onclick="mapinit()" />     }   </div>   <script type="text/javascript">      function mapinit() {         $.ajax({             url: '@url.action("service", "request")',             // datatype: "json",             data: $('form').serialize(), // update             type: "post",             // contenttype: 'application/json; charset=utf-8'         });     }   </script> 

controller

[httppost] public partialviewresult service(newrequestviewmodel model) {     return partialview("_newservice", model); } 

here suggestion how resolve task:

  1. you have change type of button on second partial view form button type: <input type="button" value="save" class="btn btn-default" onclick="mapinit()" /> step necessary not clean form when original submit-event works out after ajax method.
  2. in mapinit function should add property success , write function there update second partial form hand way:

    function mapinit() {     $.ajax({         url: '@url.action("service", "request")',         // datatype: "json",         data: $('form').serialize(), // update         type: "post",         success: function (data)         {             var html = $(data).find('form').html();             $('#newrequest').find('form').html(html);         }     }); } 

thus, providing first partial form has same fields, you'll able fill second form want.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -