I have a main view and two partial views. I need to be able to populate the viewmodel with values ββin my first partial view and pass the viewmodel to the second partial view when the button is clicked. The button is in a second partial view. I wrote a javascript function for this, but the viewmodel is empty when I test the controller method. As you can see in the screenshot below the service window - this is the second partial view
First partial view

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 this type: "POST", // contentType: 'application/json; charset=utf-8' }); } </script>
controller
[HttpPost] public PartialViewResult Service(NewRequestViewModel model) { return PartialView("_NewService", model); }
source share