Skip to main content
 首页 » 编程设计

c#之对于 ModelState 无效的项目,不会显示验证消息

2024年02月06日14hnrainll

当我将设备的名称编辑为空/null 时,ModelState 无效,我如何才能返回所有设备的列表,但显示一个编辑过的设备,并显示一条必须输入名称的验证消息?

目前验证错误仅在设备表上可见,但我想在 TextBoxFor Helper 的左侧显示验证消息。

为什么验证消息在那里不可见?

查看

@model IEnumerable<MVCTest.Models.DeviceViewModel> 
 
@{ 
    ViewBag.Title = "Index"; 
} 
 
<h2>Index</h2> 
 
<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
 
@Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
<table class="table"> 
    <tr> 
        <th> 
 
        </th> 
        <th></th> 
    </tr> 
 
    @foreach (var item in Model) 
    { 
        using (Html.BeginForm("Save", "Device")) 
        { 
            <tr> 
                <td style="background: alice;"> 
                    @Html.ValidationMessageFor(m => item.Name) 
                </td> 
                <td> 
                    @Html.TextBoxFor(modelItem => item.Name) 
                </td> 
                <td> 
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }) | 
                    @Html.ActionLink("Details", "Details", new { id = item.Id }) | 
                    @Html.ActionLink("Delete", "Delete", new { id = item.Id }) 
                </td> 
                <td> 
                    <input type="submit" value="Save" /> 
                </td> 
            </tr> 
        } 
    } 
</table> 

Controller

public class DeviceController : Controller 
    { 
        private List<DeviceViewModel> GetDevices() 
        { 
            return new List<DeviceViewModel> 
            { 
                new DeviceViewModel{ Id = 1, Name = "Test 1"}, 
                new DeviceViewModel{ Id = 1, Name = "Test 2"}, 
                new DeviceViewModel{ Id = 1, Name = "Test 3"}, 
            }; 
        } 
 
        // GET: Device 
        public ActionResult Index() 
        { 
            var devices = GetDevices(); 
            return View(devices); 
        } 
 
        public ActionResult Save(DeviceViewModel viewModel) 
        { 
            if (ModelState.IsValid) 
            { 
                // save to db 
                return RedirectToAction("Index"); 
            } 
 
            return View("Index", GetDevices()); 
        } 
    } 

View 模型

  public class DeviceViewModel 
    { 
        public int Id { get; set; } 
 
        [Required(ErrorMessage = "You must enter a name!")] 
        public string Name { get; set; } 
 
    } 

请您参考如下方法:

问题是这一行:

@Html.ValidationMessageFor(m => item.Name,"") 

它使用空白错误消息作为 specified message .

将其更改为:

@Html.ValidationMessageFor(m => item.Name) 

或者:

@Html.ValidationMessageFor(m => item.Name,"A custom error message.") 

更新

您还需要将 viewModel 返回到 View ,而不是在发布后创建一个新 View 。

public ActionResult Save(DeviceViewModel viewModel) 
{ 
    if (ModelState.IsValid) 
    { 
        // save to db 
        return RedirectToAction("Index"); 
    } 
 
    return View("Index", viewModel); 
} 

为了将您的收藏发回服务器,您需要使用 for 循环而不是 foreach。

参见此处:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

@foreach (var i = 0; i< Model.Count(); i++) 
    { 
        using (Html.BeginForm("Save", "Device")) 
        { 
            <tr> 
                <td style="background: alice;"> 
                    @Html.ValidationMessageFor(m => Model[i].Name) 
                    @Html.HiddenFor(m => m.Id) 
                </td> 
                <td> 
                    @Html.TextBoxFor(modelItem =>  Model[i].Name) 
                </td> 
                <td> 
                    @Html.ActionLink("Edit", "Edit", new { id =  Model[i].Id }) | 
                    @Html.ActionLink("Details", "Details", new { id =  Model[i].Id }) | 
                    @Html.ActionLink("Delete", "Delete", new { id =  Model[i].Id }) 
                </td> 
                <td> 
                    <input type="submit" value="Save" /> 
                </td> 
            </tr> 
        } 
    } 

我相信您需要在保存数据时将集合发回服务器,这里有一个可编辑的文本框 @Html.TextBoxFor(modelItem => item.Name)您现有的代码。