Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Passing ViewModel to controller method using JQuery Ajax
    text
    copied!<p>I'm trying to pass a form's data to my controller method using JQuery Ajax, but I'm not sure how you do this as my ViewModel is null when I use debugger on the Controller Side.</p> <p>My ViewModel is:</p> <pre><code>public class PremisesViewModel { public string createPremisesErrorMessage { get; set; } public string updatePremisesErrorMessage { get; set; } public SelectList listOfCounties = Initialise.countiesSelectList; public Premise premises { get; set; } } </code></pre> <p>where premises is an entity/table in my database.</p> <p>The form contains the fields in the Premises table.</p> <p>In my javascript function I do this:</p> <pre><code> var premisesViewModel = { Id: 0, PremisesDescription: $('#premises_PremisesDescription').val(), OrdnanceSurveyReference: $('#premises_OrdnanceSurveyReference').val(), PartRestrictedNotes: $('#premises_PartRestrictedNotes').val(), NatureOfPremises: $('#premises_NatureOfPremises').val(), AddressLine1: $('#premises_AddressLine1').val(), AddressLine2: $('#premises_AddressLine2').val(), Town: $('#premises_Town').val(), CountyId: $('#premises_CountyId').val(), Postcode: $('#premises_Postcode').val() } alert(form.serialize); $.ajax({ url: form.attr('action'), type: 'POST', dataType: "json", contentType: 'application/json', data: JSON.stringify(premisesViewModel), success: function (data) { alert('done'); } }) </code></pre> <p>However, when I check the viewModel parameter in my method, it is null:</p> <pre><code> [HttpPost] public JsonResult Create(PremisesViewModel pvm) { return null; } </code></pre> <p>Any ideas on how to map this so that the viewmodel is bound correctly. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload