c# - 模型属性作为空值传递给操作

标签 c# asp.net asp.net-mvc asp.net-mvc-4 razor

我试图将类型为“EmployeeDocument”的项目传递给我的操作,但所有值都以空值形式返回,我不确定为什么。我尝试过传递整个模型以及部分模型,但它们都是空的,我不确定为什么会发生这种情况。

这是我的观点:

    <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>

我关注的是表格中的替换操作链接。

这就是我的操作:

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

操作中的所有参数均为空。这些是有原因的吗?

最佳答案

您正在尝试回发对象集合,为了使其正常工作,您需要使用 for 循环并使用隐藏输入对属性进行索引,如下所示:

@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>
                }
}

您还需要一个相应的 post 方法来接受传回的模型:

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

关于c# - 模型属性作为空值传递给操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591435/

相关文章:

asp.net - 自动生成的 visual studio &lt;meta&gt; 标签有什么用吗

c# - 如何在 MVC4 的 UserProfile 中创建自定义附加字段

c# - 在带有 HtmlAttributes 的 MVC Html.ActionLink 上使用 @class 和自定义类失败

c# - Mapnik.NET 图层数据源路径

c# - 如何在泛型中使用值类型

c# - Javascript 在 ASP.NET Web 应用程序中确认框重定向?

asp.net - 如何创建星级评定系统?

jquery - 从部分 View 返回纯 HTML

c# - 异步等待返回任务

c# - Fody 仅在 MSBuild 16 及更高版本上受支持。当前版本 : 15