在我的MVC 5应用程序中,我有多个基于分组的表。在表格中,可以更改某些值。当FirstName字段为空时。当我提交表单时,客户端验证不起作用,我不知道原因。
型号:
public class ResourceModel
{
public int Id { get; set; }
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
控制器:
public ActionResult Employee()
{
List<ResourceModel> resources = new List<ResourceModel>();
resources.Add(new ResourceModel() { Id = 1, FirstName = "Piet", LastName = "Willemse" });
resources.Add(new ResourceModel() { Id = 2, FirstName = "Jan", LastName = "Janssen" });
resources.Add(new ResourceModel() { Id = 3, FirstName = "Willem", LastName = "Willemse" });
resources.Add(new ResourceModel() { Id = 4, FirstName = "Hendrik", LastName = "Willemse" });
resources.Add(new ResourceModel() { Id = 5, FirstName = "James", LastName = "Janssen" });
return View(resources.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Validation(List<ResourceModel> resource)
{
if(ModelState.IsValid != true)
{
return RedirectToAction("Employee");
}
return RedirectToAction("Employee");
}
查看:
@model List<EmployeeApp.Models.ResourceModel>
@{
ViewBag.Title = "Employee";
}
<h2>Employee</h2>
@using (Html.BeginForm("Validation", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
int i = 0;
foreach (var grp in Model
.OrderBy(m => m.LastName)
.GroupBy(m => new { m.LastName }))
{
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
@foreach (var itm in grp)
{
<tr>
<td>
<input type="hidden" value="@i" name="Index" />
@Html.TextBoxFor(m => itm.FirstName, new { Name = "[" + i + "].FirstName", @id = "[" + i + "].FirstName" })
</td>
<td>
@Html.TextBox("[" + i + "].LastName", itm.LastName)
@Html.ValidationMessage("[" + i + "].LastName")
</td>
</tr>
i++;
}
</tbody>
</table>
}
<input type="submit" value="submit" />
}
当我将观点更改为:
<table>
<thead><tr><td>First name</td><td>Last name</td></tr><thead>
<tbody>
for(int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@Html.TextBoxFor(m => m[i].FirstName)</td>
<td>@Html.TextBoxFor(m => m[i].LastName)
@Html.ValidationMessageFor(m => m[i].LastName)</td>
</tr>
}
</tbody>
</table>
客户端验证正在运行。
答案 0 :(得分:0)
根据这个问题的评论,我找到了一个适合我的解决方案。
我已将.OrderBy放在控制器中并更改了视图:
控制器
public ActionResult Employee()
{
List<ResourceModel> resources = new List<ResourceModel>();
resources.Add(new ResourceModel() { Id = 1, FirstName = "Piet", LastName = "Willemse" });
resources.Add(new ResourceModel() { Id = 2, FirstName = "Jan", LastName = "Janssen" });
resources.Add(new ResourceModel() { Id = 3, FirstName = "Willem", LastName = "Willemse" });
resources.Add(new ResourceModel() { Id = 4, FirstName = "Hendrik", LastName = "Willemse" });
resources.Add(new ResourceModel() { Id = 5, FirstName = "James", LastName = "Janssen" });
var orderedResources = resources.OrderBy(r => r.LastName).Select(m => new ResourceModel()
{
Id = m.Id,
LastName = m.LastName,
FirstName = m.FirstName
}).ToList();
return View(orderedResources);
}
查看
@model List<EmployeeApp.Models.ResourceModel>
@{
ViewBag.Title = "Employee";
}
<h2>Employee</h2>
@using (Html.BeginForm("Validation", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
int x = 0;
foreach(var grp in Model.GroupBy(m => new { m.LastName }))
{
<h3>@grp.Key.LastName</h3>
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
@for(int i = 0; i < @Model.Where(m => m.LastName == grp.Key.LastName).Count(); i++)
{
<tr>
<td>
<input type="hidden" value="@x" name="Index" />
@Html.TextBoxFor(r => r[x].FirstName)
</td>
<td>
@Html.TextBoxFor(r => r[x].LastName)
@Html.ValidationMessageFor(r => r[x].LastName)
</td>
</tr>
x++;
}
<tr>
<td>
<input type="hidden" value="@x + 1" name="Index" />
<input type="text" name="[@x + 1].FirstName" />
</td>
<td>
<input data-val="true" data-val-required="The field is required" name="[@x + 1].LastName" type="text" value="">
<span class="field-validation-valid errorText" data-valmsg-for="[@x + 1].LastName" data-valmsg-replace="true"></span>
</td>
</tr>
</tbody>
</table>
}
<input type="submit" value="submit" />
}