user3788671
user3788671

Reputation: 2047

Model Properties passing as nulls to Action

I am trying to pass an item with the type "EmployeeDocument" to my action but all of values are coming back as nulls and I am not sure why. I have tried passing the entire model as well as just pieces and they are all null and I am not sure why this is happening.

Here is what my view looks like:

    <table id="results-table" class="table table-striped table-bordered table-condensed">
        <thead>
            <tr>
                <th>
                    <u>@Html.DisplayName("Title")</u>
                </th>
                <th>
                    <u>@Html.DisplayName("Document Type")</u>
                </th>
                <th>
                    <u>@Html.DisplayName("Created By")</u>
                </th>
                <th>
                    <u>@Html.DisplayName("Created Date")</u>
                </th>
                <th style="width: 180px;"></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DocumentType)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.CreatedBy)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.CreatedDate)
                    </td>
                    <td><a href="@item.FullUrl">View</a> | @Html.ActionLink("Replace", "ReplaceEmployeeDocument", "Employee",new {title = item.Title, doctype = item.DocumentType, createdDate = item.CreatedDate,createdBy = item.CreatedBy, fullUrl = item.FullUrl}) | @Html.ActionLink("Delete", "DeleteEmployeeDocument",new {fileName = item.FullUrl, employeeNo = item.EmployeeNumber})</td>
                </tr>
            }
        </tbody>
    </table>

I am focusing on the replace action link in the table.

This is what my action looks like:

    [HttpGet]
    public ActionResult ReplaceEmployeeDocument(string title, string doctype, DateTime createdDate, string createdBy, string fullUrl)
    {
        var doc = new EmployeeDocument();
        return PartialView(doc);
    }

All of the parameters in the action are null. Is there a reason why these are like that?

Upvotes: 2

Views: 70

Answers (2)

hutchonoid
hutchonoid

Reputation: 33306

You are trying to post back a collection of objects, in order for this to work you need to use a for loop and index the properties with hidden inputs as follows:

@using (Html.BeginForm("ReplaceEmployeeDocument", "Controller"))
{
    @for(var i = 0; i < Model.Count(); i++)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(m => m[i].Title)
                            @Html.DisplayFor(m => m[i].Title)
                        </td>
                        <td>
                            @Html.HiddenFor(m => m[i].DocumentType)
                            @Html.DisplayFor(m => m[i].DocumentType)
                        </td>
                        <td>
                            @Html.HiddenFor(m => m[i].CreatedBy)
                            @Html.DisplayFor(m => m[i].CreatedBy)
                        </td>
                        <td>
                            @Html.HiddenFor(m => m[i].CreatedDate)
                            @Html.DisplayFor(m => m[i].CreatedDate)
                        </td>
                        <td><a href="@item.FullUrl">View</a> | <input type="submit" value="Replace"/></td>
                    </tr>
                }
}

You also need a corresponding post method that accepts the model being passed back:

[HttpPost]
public ActionResult ReplaceEmployeeDocument(EmployeeDocument model)
{
    var doc = new EmployeeDocument();
    return PartialView(doc);
}

Upvotes: 2

Louis
Louis

Reputation: 583

You dont seem to be using any @Html.EditorFor which would generate input fields, and you dont have any form or javascript that would send your arguments in either GET or POST method. Therefore none of your fields are sent to the method in the controller. You should wrape the code inside the foreach in a

@Html.BeginForm() {}

Upvotes: 1

Related Questions