Leron
Leron

Reputation: 9876

Determine the type of an action method argument recieving data from strongly typed view

I have the following problem - I am developing an ASP.NET MVC 3 application and I have view which is strongly typed. Because of the complexity of the data the model in the view looks like this :

@model List<List<DataAccess.MCS_DocumentFields>[]>

Then I render the view like this :

@using (Html.BeginForm("ActionMethodName", "Forms"))
{
    <table border="1">
                <tbody>
                    @for (int i = 0; i < Model.Count(); i++)
                    {
                        if (Model[i][0][0].ContentTypeId == 1)
                        {
                            @Html.Partial("_PartialHeader", Model[i])
                        }
                        else if (Model[i][0][0].ContentTypeId == 2)
                        {
                            @Html.Partial("_PartialDrawing", Model[i])
                        }
                        else if (Model[i][0][0].ContentTypeId == 3)
                        {
                            @Html.Partial("_PartialBody", Model[i])
                        }
                        else if (Model[i][0][0].ContentTypeId == 4)
                        {
                            @Html.Partial("_PartialFooter", Model[i])
                        }
                    }
                </tbody>
            </table>
            <button type="submit">Save</button>
}

and this is one of my partial views :

if (string.IsNullOrEmpty(item.FieldValue))
                    {                           
                        <td colspan="2">
                            @Html.DisplayFor(y => y[i][0].QuestionText)
                            @Html.HiddenFor(y => y[i][0].QuestionText)
                        </td>           
                    }
                    else
                    { 
                        <td colspan="2">
                            @Html.DisplayFor(y => y[i][0].QuestionText)
                            @Html.HiddenFor(y => y[i][0].QuestionText)
                            :
                            @Html.DisplayFor(y => y[i][0].FieldValue)
                            @Html.HiddenFor(y => y[i][0].FieldValue)
                        </td>           
                    }

This is just a snippet, what I want to say is that my table has at least 8-9 rows and almost each row has @Html.HiddenFor so I expect to get data when I'm submiting the form.

In my controller I have this method :

[HttpPost]
public ActionResult ActionMethodName(List<MCS_DocumentFields>[] collection)
{
    var test = collection;
    List<MCS_Documents> model = DocumentService.All().ToList();
    return View("Index", model);
}

I tried a lot of different types for the collection argument. Most of the time I get null, at best I get the first two row (No idea why exactly the first two) but nothing more. If I use FormCollection then I have all my submitted data, but it's not related to my MCS_DocumentFields class in any way.

I don't know what I'm doing wrong. I don't think that the type for the method should be guessed I think it must be determined by something and if there is some problem in code (most possibly in the partial view) because I've posted here almost all of my main view, then any suggestions why my logic is not working and why I can't bind the data to the original type of the model?

Upvotes: 2

Views: 68

Answers (1)

icesar
icesar

Reputation: 496

I suggest you to look at the BeginCollectionItem package (http://nuget.org/packages/BeginCollectionItem/), allthough I can see that your model is really complex (3 dimensions), sou you'll have to nest them in a really nasty manner.

Also you should consider (due to your model complexity) creating a custom model binder (http://www.codeproject.com/Articles/605595/ASP-NET-MVC-Custom-Model-Binder).

Just one last remark - your model in View differs from your model that you expect in action:

List<MCS_DocumentFields>[] collection != List<List<DataAccess.MCS_DocumentFields>[]>

Ivan

Upvotes: 1

Related Questions