0

I have a ViewModel Like this :

public class SaveWorkOrderViewModel
{
    public Guid WorkOrderId { get; set; }
    public List<SaveWorkOrderDelayReasonViewModel> DelayReasons { get; set; }
}
public class SaveWorkOrderDelayReasonViewModel
{
    public Guid DelayReasonId { get; set; }
    public string Title { get; set; }
}

and I have controller that get SaveWorkOrderViewModel as param Like this :

[HttpPost]
public virtual JsonResult Save(SaveWorkOrderViewModel saveWorkOrderViewModel)
{
    ....
}

to bind data to this I tried like this :

var workOrderId = $(this).data('workOrderId'); // return guid
var DelayReasons = $("#drpdelay").select2("val"); // return array of guid
var saveWorkOrderViewModel = {
    workOrderId: workorderId,
    DelayReasons: {
        DelayReasonId:DelayReasons 
    }
};

but when Post Page , I getting null for DelayReasons.

I couldn't find any similar post to solve this .

updated :

My script for send ajax call is :

$.ajax({
    url: '@Url.Action(MVC.Admin.WorkOrder.Save())',
    data: { saveWorkOrderViewModel },
    type: "POST",
    success: function (data) {
        showMessage(data.message, data.notificationType);
        if (data.result) {
            RefreshKendoGrid('woGrid');
        }
    },
    error: function (response) { }
});

Finally I changed scrips like this and its work fine :

var fields = [];
$(delays).each(function (index, value) {
    var obj = {};
    obj["DelayReasonId"] = value;
    obj["Title"] = null;
    fields.push(obj);
});

var saveWorkOrderViewModel = {
    workOrderId: workorderId,
    DelayReasons: fields
};
5
  • Are you submitting this using ajax? If so, show your code for the script. And SaveWorkOrderDelayReasonViewModel contains a property Title. Are you not submitting that? Commented Apr 8, 2017 at 7:04
  • yes I use ajax . not I dont fill Title field . Commented Apr 8, 2017 at 7:13
  • The show your code :) Commented Apr 8, 2017 at 7:14
  • @StephenMuecke updated Commented Apr 8, 2017 at 7:23
  • Not quite sure why you claiming and its work fine. You will get a collection of SaveWorkOrderDelayReasonViewModel buttThe values of DelayReasonId will not be bound :) Commented Apr 8, 2017 at 7:47

1 Answer 1

1

You need to set the contentType option to use json, and stringify your data

....
var saveWorkOrderViewModel = {
    workOrderId: workorderId,
    DelayReasons: fields
};

$.ajax({
   url: '@Url.Action(MVC.Admin.WorkOrder.Save())',
   data: JSON.stringify(saveWorkOrderViewModel),
   type: 'POST',
   contentType: 'application/json; charset=UTF-8',
   success: function (data) {
       ....

Alternatively, to use your current ajax options which uses the default application/x-www-form-urlencoded; charset=UTF-8 for contentType, you would need to generate the name/value pairs for the collection with indexers, for example (where xxxx are your Guid values)

var saveWorkOrderViewModel = {
    workOrderId: workorderId,
    DelayReasons[0].DelayReasonId: xxxx,
    DelayReasons[1].DelayReasonId: xxxx,
    ....
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.