我正在尝试使用表单和POST方法将视图中的客户列表发送到控制器。但是,控制器中的模型为空。
这是我的ViewModel:
public class CustomerViewModel
{
public List<Customer> Customers { get; set; }
}
这是视图:
@model CustomerViewModel
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
@Html.BeginForm("SendSMS", "Customers", FormMethod.Post)
{
<table class="table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Customers)
{
<tr>
<td>
@Html.EditorFor(modelItem => item.IsSelected)
@Html.HiddenFor(modelItem => item.Email)
@Html.HiddenFor(modelItem => item.FirstName)
@Html.HiddenFor(modelItem => item.LastName)
@Html.HiddenFor(modelItem => item.PhoneNumber)
@Html.HiddenFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a> |
<a asp-action="Message" asp-route-id="@item.ID">Message</a>
</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Save" />
}
这是控制器动作:
[HttpPost]
public IActionResult SendSMS(CustomerViewModel model)
{
StringBuilder sb = new StringBuilder();
var customers = model.Customers;
foreach (var i in customers)
{
if (i.IsSelected)
{
sb.Append(i.Email + ",");
}
}
ViewBag.Data = sb.ToString();
return View(model);
}
我收到一个运行时错误消息,指出模型.SendSMS ActionResult中的客户为空。
任何帮助将不胜感激。