gpt4 book ai didi

asp.net-mvc-3 - DropDownListFor Unobtrusive Validation required 没有得到正确的属性

转载 作者:行者123 更新时间:2023-12-04 11:43:17 28 4
gpt4 key购买 nike

This Question类似,但接受的答案解决了服务器端,我对客户端解决方案感兴趣。

鉴于此 ViewModel

public class MyViewModel
{
public string ID { get; set; }

[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Some Choice")]
public int SomeChoice{ get; set; }
[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Keyword")]
public string Keyword { get; set; }
}

和 Razor
<div>
@Html.LabelFor(model => model.SomeChoice, new { @class = "label" })
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...")
@Html.ValidationMessageFor(model => model.SomeChoice)
</div>

并假设 ViewBag.SomeChoice 包含一个选择列表

呈现的 html 没有得到 data-val="true"data-val-required="I DEMAND YOU MAKE A CHOICE!"其中的属性,如 @Html.EditorFor(model => model.Keyword) 或 @Html.TextBoxFor 将呈现。

为什么?

像这样添加一个class =“required”
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...", new { @class = "required" })

它使用 jQuery Validation 类语义并在提交时阻止但不显示消息。我可以做这种事情
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...", new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "I DEMAND YOU MAKE A CHOICE!" } })

它将把正确的属性放在那里,并阻止提交并显示消息,但没有利用我在 ViewModel 上的RequiredAttribute ErrorMessage

那么有没有人写过一个 DropDownListFor,它在验证方面的行为与其他 HtmlHelpers 一样?

编辑
这是我的确切代码

在 HomeController.cs
  public class MyViewModel
{
[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Some Choice")]
public int? SomeChoice { get; set; }
}


public ActionResult About()
{
var items = new[] { new SelectListItem { Text = "A", Value = "1" }, new SelectListItem { Text = "B", Value = "2" }, new SelectListItem { Text = "C", Value = "3" }, };
ViewBag.SomeChoice = new SelectList(items,"Value", "Text");
ViewData.Model = new MyViewModel {};
return View();
}

关于.cshtml
@using Arc.Portal.Web.Host.Controllers
@model MyViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
<div>
@Html.LabelFor(model => model.SomeChoice)
@Html.DropDownListFor(model => model.SomeChoice, (SelectList)ViewBag.SomeChoice, "Select...")
@Html.ValidationMessageFor(model => model.SomeChoice)
</div>

<button type="submit">OK</button>
}

这是渲染的代码
<form action="/Home/About" method="post">    <div>
<label for="SomeChoice">Some Choice</label>
<select id="SomeChoice" name="SomeChoice"><option value="">Select...</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<span class="field-validation-valid" data-valmsg-for="SomeChoice" data-valmsg-replace="true"> </span>
</div>
<button type="submit">OK</button>
</form>

它发回给我的 Controller ......这不应该发生

最佳答案

只需在您将下拉列表绑定(bind)到 View 模型的属性上使用可为空的整数:

[Required(ErrorMessage = "I DEMAND YOU MAKE A CHOICE!")]
[Display(Name = "Some Choice")]
public int? SomeChoice { get; set; }

此外,为了获得适当的不显眼的 HTML5 data-* 属性,下拉列表必须在表单内:
@model MyViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
<div>
@Html.LabelFor(model => model.SomeChoice, new { @class = "label" })
@Html.DropDownListFor(
model => model.SomeChoice,
Model.ListOfChoices,
"Select..."
)
@Html.ValidationMessageFor(model => model.SomeChoice)
</div>

<button type="submit">OK</button>
}

你也会注意到我摆脱了 ViewBag (我根本无法忍受)并将其替换为 View 模型上的相应属性,该属性将包含下拉菜单的可能选项。

关于asp.net-mvc-3 - DropDownListFor Unobtrusive Validation required 没有得到正确的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828957/

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