0

This is my model

Model

public class XPersonContent
{
    public string PersonType { get; set; }
    public int PersonId { get; set; }

}

public class XViewModel
{
    public List<XPersonContent> XPersonList { get; set; }
    public string XContent { get; set; }
    public sbyte CommentEnabled { get; set; }

}

Controller

[HttpPost]
public JsonResult AddXViewModel ( XViewModel testModel)
{

}

Form using MVC that submits to controller

function submitForm() {

    var xpersonContent=[Object { Id=2934109,  Type="us"}, Object { Id=2913974,  Type="us"}, Object {Id=2912159,  Type="us"}]
    var xContent= "test";
    var CommentEnabled= false;

    var dataString = {
        XPersonList:xpersonContent,
        XContent: xContent,
        CommentEnabled: true
    };

    $.ajax({
        type: "POST",
        url: "/AjaxPostDemo/AddXViewModel",
        data: JSON.stringify(dataString ),
        cache: false,
        dataType: "json",
        success: function (data) {
            $("#ajaxPostMessage").empty();
            $("#ajaxPostMessage").html(data.Message + " : " + data.Date);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });
}

> Question

How do i build the object XViewModel to pass back to the controller. i have them in three different variables i have tried to do this dataString = { XPersonList:xpersonContent, XContent: xContent, CommentEnabled: true }; but its not working..

4

2 回答 2

0

I had to add contentType: "application/json; charset=utf-8", in my ajax request SMH

于 2013-03-31T01:59:06.473 回答
0

Try change to this

var xpersonContent=[{"XPersonContent": {"Id": 2934109, "Type": "us"}}, {"XPersonContent": {"Id": 2913974, "Type": "us"}}, {"XPersonContent": {"Id": 2912159, "Type": "us"}}]
于 2013-03-31T01:59:54.563 回答