0

I created a link <a href="#" id="ajaxPost">send model as json</a> which sends this json to my controller:

{
   "textFields":[
      {
         "name":"text",
         "xPath":"/test/text"
      },
      {
         "name":"text2",
         "xPath":"/test/text2"
      }
   ]
}
public PartialViewResult addTextField(List<AddTextFieldModel> textFields) {
    return PartialView("_AddTextField", new AddTextFieldModel("hallo", "123"));
}

via jQuery ajax.

$(document).ready(function () {
        var textFields = [
            { name: "text", xPath: '/test/text' },
            { name: "text2", xPath: '/test/text2' }
        ];      
        
        //textFields = JSON.stringify({ 'textFields': textFields });
       
    $("#ajaxPost").on("click", function (e) {
        e.preventDefault();
        $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'html',
            type: 'POST',
            url: '@Url.Action("addTextField")',
            data: '{ "textFields":' + JSON.stringify(textFields) + '}',
            success: function (data) {
                $('#mainDiv').html(data);
            },
            failure: function (response) {
                alert(response);
                $('#mainDiv').html(response);
            }
        });
       
    });
 });

The problem I have; the .net mvc 4.5.2 does not map the json object to the list of my model:

public class AddTextFieldModel {
    public String Name { get; set; }
    public String XPath { get; set; }

    public AddTextFieldModel(string name, string xPath) {
        Name = name;
        XPath = xPath;
    }
}

Besides how to replace the jQuery ajax call with @Ajax.ActionLink("Add", "addTextField", ???, new AjaxOptions { HttpMethod = "Post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "mainDiv" }) ?

jbafford
  • 5,528
  • 1
  • 24
  • 37
StellaMaris
  • 877
  • 2
  • 12
  • 29
  • 2
    AFAIK you have to have a parameterless constructor in the model class for the model binding to work – shakib Jan 22 '16 at 13:13
  • your trying to model bind from view to controller input. Not MapJson to model.... if you are trying to do that then you would need to write your own module to handle this for you.(really don't think that is what you are trying to do) – Seabizkit Jan 22 '16 at 13:13
  • google "mvc view to model binding"... and im sure you will come right. its how the data is serialized from the form...POST/GET... you can simulate with ajax, so the values are bound in the controller. – Seabizkit Jan 22 '16 at 13:15
  • I googled it and find some [links](http://stackoverflow.com/questions/13242414/passing-a-list-of-objects-into-an-mvc-controller-method-using-jquery-ajax) and I cant see the difference to my methodology. – StellaMaris Jan 22 '16 at 13:18
  • @shakib, thx I supposed that the default constructer is there anyway. Now its working. – StellaMaris Jan 22 '16 at 13:20
  • You should send the JSON as a string to your `Controller`, then in your Controller use `JsonConvert.DeserializeObject>(jsonString)`. You will need `Json.NET` for this. – Rosdi Kasim Jan 22 '16 at 13:21
  • @StellaMaris you are welcome. Posted as answer – shakib Jan 22 '16 at 13:36
  • @Rosdi Kasim, thx that is a nice workaround. – StellaMaris Jan 22 '16 at 13:37

1 Answers1

1

Asp.net MVC model binding requires a parameterless constructor when a constructor is defined in the class. Changing the following should solve the issue.

public class AddTextFieldModel{
    public String Name { get; set; }
    public String XPath { get; set; }

    //parameterless constructor
    public AddTextFieldModel() {
    }

    public AddTextFieldModel(string name, string xPath) {
        Name = name;
        XPath = xPath;
    }
}
shakib
  • 5,449
  • 2
  • 30
  • 39