我试图将类型为“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/