gpt4 book ai didi

javascript - 添加 MVC ValidationMessageFor 以分隔 Div 标签

转载 作者:行者123 更新时间:2023-11-30 12:19:22 24 4
gpt4 key购买 nike

有没有办法将所有 @Html.ValidationMessageFor block 添加到 MVC 父 _layout.cshtml 页面页脚中的 div 容器?下面的代码显示了底部 div 中的验证消息,但我想使用 jQuery 将它们显示在 _Layout.cshtml DIV 的页脚中。

@model Proj.ViewModels.ApplicationControllerViewModel
@Html.ValidationSummary(true)

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Details" }))
{
<div id="divApplication">
<div class="editor-label">
@Html.LabelFor(model => model.Applicaiton.Name, new { @class = "formLabel" })
@Html.HiddenFor(model => model.Applicaiton.ID, new { @class = "Application-ID" })
@Html.TextBoxFor(model => model.Applicaiton.Name, new { @class = "Application-Name" })
</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.ControllerList, new { @class = "formLabel" })
@Html.DropDownList("ControllerList", Model.ControllerList, new { @class = "Application-ControllerList"})
</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.Applicaiton.Abbreviation, new { @class = "formLabel" })
@Html.TextBoxFor(model => model.Applicaiton.Abbreviation, new { @class = "Application-Abbreviation" })
</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.Applicaiton.Description, new { @class = "formLabel" })
@Html.TextAreaFor(model => model.Applicaiton.Description, new { @class = "Application-Description", rows = 10, cols = 60 })
</div>
<br />
<div class="editor-label">
@Html.Label("Deployed Date", new { @class = "formLabel" })
@Html.TextBoxFor(model => model.Applicaiton.DeployedOn, "{0:MM/dd/yyyy}", new { @class = "Application-DeployedOn" })
</div>
<div>
@Html.ValidationMessageFor(model => model.Applicaiton.Name)
@Html.ValidationMessageFor(model => model.Applicaiton.Abbreviation)
@Html.ValidationMessageFor(model => model.Applicaiton.Description)
@Html.ValidationMessageFor(model => model.Applicaiton.DeployedOn)
</div>
</div>
}

最佳答案

我会使用内置功能通过 _Layout.cshtml 中的 @RenderSection 执行此操作:

_Layout.cshtml(简化版举例)

<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<div id="content">
@RenderBody()
</div>
<div>
@RenderSection("ValidationMessages", false)
</div>

</body>
</html>

查看

@model Proj.ViewModels.ApplicationControllerViewModel
@Html.ValidationSummary(true)

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Details" }))
{
<div id="divApplication">
<div class="editor-label">
@Html.LabelFor(model => model.Applicaiton.Name, new { @class = "formLabel" })
@Html.HiddenFor(model => model.Applicaiton.ID, new { @class = "Application-ID" })
@Html.TextBoxFor(model => model.Applicaiton.Name, new { @class = "Application-Name" })
</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.ControllerList, new { @class = "formLabel" })
@Html.DropDownList("ControllerList", Model.ControllerList, new { @class = "Application-ControllerList"})
</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.Applicaiton.Abbreviation, new { @class = "formLabel" })
@Html.TextBoxFor(model => model.Applicaiton.Abbreviation, new { @class = "Application-Abbreviation" })
</div>
<br />
<div class="editor-label">
@Html.LabelFor(model => model.Applicaiton.Description, new { @class = "formLabel" })
@Html.TextAreaFor(model => model.Applicaiton.Description, new { @class = "Application-Description", rows = 10, cols = 60 })
</div>
<br />
<div class="editor-label">
@Html.Label("Deployed Date", new { @class = "formLabel" })
@Html.TextBoxFor(model => model.Applicaiton.DeployedOn, "{0:MM/dd/yyyy}", new { @class = "Application-DeployedOn" })
</div>
</div>
}

@section ValidationMessages {
@Html.ValidationMessageFor(model => model.Applicaiton.Name)
@Html.ValidationMessageFor(model => model.Applicaiton.Abbreviation)
@Html.ValidationMessageFor(model => model.Applicaiton.Description)
@Html.ValidationMessageFor(model => model.Applicaiton.DeployedOn)
}

关于javascript - 添加 MVC ValidationMessageFor 以分隔 Div 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31542911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com