1

Closed

Post Data server side automapping to complex object fails

description

Hello,

I use a complex model with multiple sub objects in a view.
Usually, if I post the whole form, the html form is automatically mapped to the model object I used in the get.
It fails with error parameter 'value' can't be null and I have no value member in my parameters.
The error is thrown after the instance of the controller to be initialized.

I Use this workaround to make my method working :
    [HttpPost, Authorize, CacheManager(enCacheMode.NoCache)]
    //public JsonResult AddUpdate(int id, AdvancedSamples_AddressesManagementModel model)
    public JsonResult AddUpdate(int id)
    {
        Contact__AdressType__ContactAddress dbContact__AdressType__ContactAddress;
        //MVC4 BugFix Model cannot be binded automatically => Old school binding...
        AdvancedSamples_AddressesManagementModel model = new AdvancedSamples_AddressesManagementModel();
        model.CurrentAddress.Id = int.Parse(Request.Form["CurrentAddress.Id"]);
        model.CurrentAddress.IsPostAddress = !(Request.Form["CurrentAddress.IsPostAddress"] == "false");
        model.CurrentAddress.IsInvoicingAddress = !(Request.Form["CurrentAddress.IsInvoicingAddress"] == "false");
        model.CurrentAddress.IsDeliveryAddress = !(Request.Form["CurrentAddress.IsDeliveryAddress"] == "false");
        model.CurrentAddress.Address_1 = Request.Form["CurrentAddress.Address_1"];
        model.CurrentAddress.Address_2 = Request.Form["CurrentAddress.Address_2"];
        model.CurrentAddress.Address_3 = Request.Form["CurrentAddress.Address_3"];
        model.CurrentAddress.ZipCode = Request.Form["CurrentAddress.ZipCode"];
        model.CurrentAddress.Town = Request.Form["CurrentAddress.Town"];
        model.CurrentAddress.Cedex = Request.Form["CurrentAddress.Cedex"];
        model.CurrentAddress.Country = Request.Form["CurrentAddress.Country"];
        string eMethod = eMethodBase + "ContactAddressGet[POST]";
        object eParameters = new { model = "model" };
        JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        using (TransactionScope TS = new TransactionScope())//Open a transaction to encapsulate multiple actions
        {
            using (DataRepositories _dataContext = new DataRepositories())
            {
....

The issue is :
Message is not clear enough for us to fix the bug.

Can you help me ?

Thanks,

Laurent
Closed Oct 23, 2013 at 8:17 AM by jacalvar
We've tried to repro the issue but we couldn't. Here are the model, controller and view we used:

Model classes:
public class Issue478Model
{
    public int Id { get; set; }
    public Address CurrentAddress { get; set; }
}

public class Address
{
    public int Id { get; set; }
    public bool IsPostAddress { get; set; }
    public bool IsInvoicingAddress { get; set; }
    public bool IsDeliveryAddress { get; set; }
    public string Address_1 { get; set; }
    public string Address_2 { get; set; }
    public string Address_3 { get; set; }
    public string ZipCode { get; set; }
    public string Town { get; set; }
    public string Cedex { get; set; }
    public string Country { get; set; }
}
Controller:
public class Issue478HomeController : Controller
{
    public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(int id, Issue478Model model)
    {
        ModelState.Clear();
        return View(model);
    }
}
View:

@model WebStack.QA.Test.MVC.BugVerificationTests.Issue478Model

<!DOCTYPE html>
<html>
<head> <title>Issue 478</title> </head> <body>
@if (Model == null)
{
    <p>Model is null</p>
}
@using (Html.BeginForm(new { action = "Add" }))
{
    @Html.EditorForModel()
    @Html.EditorFor(c => c.CurrentAddress)
    <input id="submit-button" type="submit" value="Send" />
}
</body>
</html>

comments

eilonlipton wrote Oct 3, 2013 at 8:02 PM

We think this item is fixed because we had a bug a while ago where the word "model" didn't work but I think we fixed that.

Javier, can you verify this on an MVC5 build?