Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

orchardcms/mvc null values when posting

public class Address
    {
        public string Street { get; set;}
    }


public class MyModel
    {
        public string Name { get; set;}
        public Address MyAddress { get; set;}
    }


public class MyController : Controller
{
        [HttpPost]
        public JsonResult DoStuff(MyModel model)
        {
            // model.Name has its value
            // model.MyAddress is there, but its .Street is always null
            // Do stuff
        }
}

This is how i post to the controller

var data =
    {
        __RequestVerificationToken: $("input[name=__RequestVerificationToken]").val(),
        Name: "Arnold",
        MyAddress: 
        {
               Street: "my address"
        }
    }

$.ajax({
        type: 'POST',
        url: "/myroute/dostuff", //Yes i should not use the hardcoded url but this is just for show
        data: data,
        async: false,
        success: function (result) {
            // ...
        },
        dataType: 'json',
    });

Looking at fiddler its posting the correct data.. If i look at the ModelState its only got one Key, "Name".

EDIT:

If i do this:

public class MyController : Controller
{
        [HttpPost]
        public JsonResult DoStuff(FormCollection formCollection)
        {
            // formCollection has all the data..
            // so i guess its the binding? :o any ideas how to fix?
            // Do stuff
        }
}

Upvotes: 1

Views: 112

Answers (1)

levelnis
levelnis

Reputation: 7705

What happens if you call UpdateModel(model) on the first line of the action method? It could be because model binding hasn't picked up the Address property implicitly and you need to give it an explicit nudge.

Upvotes: 1

Related Questions