ASP 核心:@Html.ValidationMessage 迁移到 asp-validation-for 或类似的标签助手
ASP CORE: @Html.ValidationMessage migration to asp-validation-for or similar tag helper
如何使用字符串值而不是表达式设置 asp-validation-for
?
我要迁移多选列表:
@Html.ListBox("Privileges", ViewBag.PrivilegesMultiSelectList as MultiSelectList)
@Html.ValidationMessage("Privileges", "")
到
<select multiple="multiple" name="Privileges" asp-items="@ViewBag.PrivilegesMultiSelectList"></select>
<span asp-validation-for="Privileges" class="text-danger"></span>
但是最后一行无效:
Error CS1061 '...Model' does not contain a definition for 'Privileges'
and no accessible extension method 'Privileges' accepting a first
argument of type '..Model' could be found (are you missing a using
directive or an assembly reference?)
为了保持一致性,我想继续使用标签助手。
此 asp-validation-for="Privileges"
试图在您的模型(不是 ViewBag)中寻找 Privileges
属性。如果它不存在,它会给你那个错误。该行相当于 ValidationMessageFor()
,据我所知,ValidationMessage()
.
在 .net 核心中没有等价物
看看 asp-validation-for tag helper,如前所述,它应该与另一个 taghelper 的名称一致。
How to setup the asp-validation-for
with string value, not with expression?
同样,在 TagHelpers 中没有 ValidationMessage()
它的等价物。所以你可以只使用 @Html.ValidationMessage()
.
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.2
it's important to recognize that Tag Helpers don't replace HTML Helpers and there's not a Tag Helper for each HTML Helper.
您也可以使用 ValidationMessage HtmlHelper
编写自己的标签助手
文档中关于 ViewBags 的一些建议:
We don't recommend using ViewBag or ViewData with the Select Tag Helper. A view model is more robust at providing MVC metadata and generally less problematic.
更好的方法:
您需要将 selected 权限添加到您希望 return 的模型中。
public class CustomViewModel {
[Required]
public string Privilege { get; set; } // update if you want to return multiple privileges
public List<SelectListItem> PrivilegesMultiSelectList { get; set; }
}
然后在您的视图中使用它 @model CustomViewModel
。
在你的 select 上使用 asp-for="Privilege"
,它变成 m => m.Privilege
。
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.2
The asp-for attribute value is a special case and doesn't require a
Model prefix, the other Tag Helper attributes do (such as asp-items)
然后你可以这样写:
<select asp-for="Privilege" asp-items="@Model.PrivilegesMultiSelectList"></select>
<span asp-validation-for="Privilege" class="text-danger"></span>
希望对您有所帮助。
如何使用字符串值而不是表达式设置 asp-validation-for
?
我要迁移多选列表:
@Html.ListBox("Privileges", ViewBag.PrivilegesMultiSelectList as MultiSelectList)
@Html.ValidationMessage("Privileges", "")
到
<select multiple="multiple" name="Privileges" asp-items="@ViewBag.PrivilegesMultiSelectList"></select>
<span asp-validation-for="Privileges" class="text-danger"></span>
但是最后一行无效:
Error CS1061 '...Model' does not contain a definition for 'Privileges' and no accessible extension method 'Privileges' accepting a first argument of type '..Model' could be found (are you missing a using directive or an assembly reference?)
为了保持一致性,我想继续使用标签助手。
此 asp-validation-for="Privileges"
试图在您的模型(不是 ViewBag)中寻找 Privileges
属性。如果它不存在,它会给你那个错误。该行相当于 ValidationMessageFor()
,据我所知,ValidationMessage()
.
看看 asp-validation-for tag helper,如前所述,它应该与另一个 taghelper 的名称一致。
How to setup the
asp-validation-for
with string value, not with expression?
同样,在 TagHelpers 中没有 ValidationMessage()
它的等价物。所以你可以只使用 @Html.ValidationMessage()
.
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.2
it's important to recognize that Tag Helpers don't replace HTML Helpers and there's not a Tag Helper for each HTML Helper.
您也可以使用 ValidationMessage HtmlHelper
编写自己的标签助手文档中关于 ViewBags 的一些建议:
We don't recommend using ViewBag or ViewData with the Select Tag Helper. A view model is more robust at providing MVC metadata and generally less problematic.
更好的方法:
您需要将 selected 权限添加到您希望 return 的模型中。
public class CustomViewModel {
[Required]
public string Privilege { get; set; } // update if you want to return multiple privileges
public List<SelectListItem> PrivilegesMultiSelectList { get; set; }
}
然后在您的视图中使用它 @model CustomViewModel
。
在你的 select 上使用 asp-for="Privilege"
,它变成 m => m.Privilege
。
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.2
The asp-for attribute value is a special case and doesn't require a Model prefix, the other Tag Helper attributes do (such as asp-items)
然后你可以这样写:
<select asp-for="Privilege" asp-items="@Model.PrivilegesMultiSelectList"></select>
<span asp-validation-for="Privilege" class="text-danger"></span>
希望对您有所帮助。