Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Passing ViewModel to controller method using JQuery Ajax

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.

My ViewModel is:

public class PremisesViewModel
{

    public string createPremisesErrorMessage { get; set; }
    public string updatePremisesErrorMessage { get; set; }

    public SelectList listOfCounties = Initialise.countiesSelectList;

    public Premise premises { get; set; }
}

where premises is an entity/table in my database.

The form contains the fields in the Premises table.

In my javascript function I do this:

   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');
        }
    })

However, when I check the viewModel parameter in my method, it is null:

  [HttpPost]
    public JsonResult Create(PremisesViewModel pvm)
    {
        return null;
    }

Any ideas on how to map this so that the viewmodel is bound correctly. Thanks

like image 889
user1079925 Avatar asked Mar 26 '12 11:03

user1079925


2 Answers

If you wanted to automatically build the model from the bound view model of a form, you can use the code from this answer https://stackoverflow.com/a/1186309 to properly serialize to a JSON object.

You'd then need to pass it as a string to your $.ajax call. All in all, very similar to what you originally had. Something like:

var premisesViewModel = $('form').serializeObject();
$.ajax({
        url: form.attr('action'),
        type: 'POST',
        dataType: "json",
        contentType: 'application/json',
        data: JSON.stringify(premisesViewModel),
        success: function (data) {
            alert('done');
        }
    });

Quite strange that there's no core function to convert to a JSON object, but there you go.

like image 60
Fiddles Avatar answered Sep 28 '22 06:09

Fiddles


Your JSON format exactly same as your model class.

Current example

public class PremisesViewModel
{

    public string createPremisesErrorMessage { get; set; }
    public string updatePremisesErrorMessage { get; set; }

    public SelectList listOfCounties = Initialise.countiesSelectList;

    public Premise premises { get; set; }
}

Your JSON like

 var premisesViewModel = {
                                    createPremisesErrorMessage : $('#premises_PremisesDescription').val(),
                                    updatePremisesErrorMessage: $('#premises_OrdnanceSurveyReference').val(),    
                                    premises : {Define more properties here as per your Premise structure}
                                }
like image 34
alok_dida Avatar answered Sep 28 '22 07:09

alok_dida