Asp.net mvc ASP.Net MVC:如何在Webgrid中使用@Html.ValidationMessageFor

Asp.net mvc ASP.Net MVC:如何在Webgrid中使用@Html.ValidationMessageFor,asp.net-mvc,webgrid,Asp.net Mvc,Webgrid,假设我的webgrid代码如下所示 假设这是我的webgrid代码 @model HelloWorldMvcApp.MainViewModel @{ Layout = null; } @{ WebGrid grid = new WebGrid(null); grid.Bind(Model.Students); } @grid.GetHtml( tableStyle: "webgrid-table",

假设我的webgrid代码如下所示

假设这是我的webgrid代码

@model HelloWorldMvcApp.MainViewModel
@{
    Layout = null;
}

@{
        WebGrid grid = new WebGrid(null);
        grid.Bind(Model.Students);

    }
    @grid.GetHtml(
            tableStyle: "webgrid-table",
            headerStyle: "webgrid-header",
            footerStyle: "webgrid-footer",
            alternatingRowStyle: "webgrid-alternating-row",
            selectedRowStyle: "webgrid-selected-row",
            rowStyle: "webgrid-row-style",
            mode: WebGridPagerModes.All,
            firstText: "<<",
            previousText: "<",
            nextText: ">",
            lastText: ">>",
            numericLinksCount: 5,
            columns:
                grid.Columns
                (
                grid.Column(columnName: "ID", header: "ID", format: @<text>@item.ID</text>,style:"SmallCols"),
                grid.Column(columnName: "FirstName", header: "First Name", format: @<text><input type="text" id="txtFirstName" value="@item.Name" class="edit-mode" /></text>,style:"NameColWidth" ),

                //grid.Column(columnName: "StateName", header: "State Name", format: @<text>@Html.DropDownListFor(x => x.SelectedState, new SelectList(Model.Students[1].States, "ID", "Name", Model.SelectedState = item.StateID), "-- Select States--", new { id = "cboState", @class = "edit-mode" })</text>,style:"NameColWidth"),

                grid.Column(columnName: "StateName", header: "State Name", format: @<text>@Html.DropDownListFor(x => x.SelectedState, new SelectList(Model.Students.SelectMany(x => x.States).Where(x => x.ID == item.StateID), "ID", "Name", Model.SelectedState = item.StateID), "-- Select States--", new { id = "cboState", @class = "edit-mode" })</text>,style:"NameColWidth"),
                grid.Column(columnName: "CityName", header: "City Name", format: @<text>@Html.DropDownListFor(x => x.SelectedCity, new SelectList(Model.Students.SelectMany(x => x.Cities).Where(x => x.StateID == item.StateID), "ID", "Name", Model.SelectedCity = item.CityID), "-- Select City--", new { id = "cboCity", @class = "edit-mode" })
                </text>,style:"NameColWidth")
                ))
@model HelloWorldMvcApp.MainViewModel
@{
布局=空;
}
@{
WebGrid=新WebGrid(空);
grid.Bind(Model.Students);
}
@grid.GetHtml(
表样式:“webgrid表”,
标题样式:“webgrid标题”,
页脚样式:“webgrid页脚”,
交替行样式:“webgrid交替行”,
selectedRowStyle:“webgrid选定行”,
行样式:“webgrid行样式”,
模式:WebGridPagerModes.All,
第一个文本:“,
数字链接:5,
柱:
网格.列
(
grid.Column(columnName:“ID”,标题:“ID”,格式:@@item.ID,样式:“SmallCols”),
grid.Column(columnName:“FirstName”,标题:“FirstName”,格式:@,样式:“NameColWidth”),
//grid.Column(columnName:“StateName”,header:“State Name”,格式:@@Html.DropDownListFor(x=>x.SelectedState,new SelectList(Model.Students[1]。States,“ID”,“Name”,Model.SelectedState=item.StateID),“--selectstates--”,new{ID=“cboState”,“@class=”编辑模式“}),style:“NameColWidth”),
grid.Column(columnName:“StateName”,header:“State Name”,格式:@@Html.DropDownListFor(x=>x.SelectedState,new SelectList(Model.Students.SelectMany(x=>x.States)。其中(x=>x.ID==item.StateID),“ID”,“Name”,Model.SelectedState=item.StateID),“--selectedstates--”,new{ID=“cboState”,“@class=”编辑模式“}),样式:“NameColWidth”),
grid.Column(columnName:“CityName”,header:“City Name”,格式:@@Html.DropDownListFor(x=>x.SelectedCity,new-SelectList(Model.Students.SelectMany(x=>x.Cities)。其中(x=>x.StateID==item.StateID),“ID”,“Name”,Model.SelectedCity=item.CityID),“--选择城市--”,new{{ID=“cboCity”,“@class=”编辑模式“)
,样式:“NameColWidth”)
))
现在告诉我如何为每个文本框和下拉列表放置
@Html.ValidationMessageFor

我的完整工作代码
https://dotnetfiddle.net/GoJeBE


请与代码示例讨论。谢谢

您应该能够在
元素之间添加
@ValidationMessageFor()
(与
@DropDownListFor()
相邻)但您无法使用小提琴-您没有表单。它对文本框不起作用,因为您需要手动生成html(没有必要的
数据val-*
属性-您需要使用
@html.TextBoxFor()
)@StephenMuecke一个我在MVC中是新手的请求。你可以用webgrid发布一个新的视图代码,包括Html.ValidationMessageFor。我可以试一下,但我怕我会出错。你能帮我吗。thanksI已经为您的文本框添加了验证,并对其进行了修改(清除内容并单击submit按钮,将显示验证错误)。但是之前我已经给了你我对WebGrid的看法,并将你与其他顶级MVC用户的意见联系在一起——这是垃圾,不要使用它。@StephenMuecke感谢你的代码,但是验证消息应该显示为红色,当验证失败时控件应该有红色边框。这并没有发生。你的dotnetfiddle中的代码缺少了什么?那是因为你创建的fiddle(我刚刚分叉)没有任何相关的css。