0

我目前得到以下片段:

@Html.EditorFor(model => model.Street, new { htmlAttributes = new { @class = "form-control tooltp", placeholder = "Rue *", autofocus = "autofocus", data_tooltip_content = "#Street_content" } })
@if (ViewData.ModelState["Street"] != null && ViewData.ModelState["Street"].Errors.Any())
{
    <div class="tooltip_templates">
        <div id="street_content">
            @Html.ValidationMessageFor(model => model.Street)
        </div>
    </div>
}

我怎样才能使它可重复使用?创建一个 MVC 助手?部分观点?这是不可能的?

我想要这种解决方案,以便我可以将它用于我所有不同的领域。我正在为工具提示消息使用 tooltipster。如果没有 if 语句,工具提示将包含一个空字符串(但仍会显示)。

我想要一个解决方案,例如我可以这样做

@ErrorHelper.ShowError("Street")
4

1 回答 1

1

我有自己的验证消息助手来自动添加引导类样式

public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string tag)
{
    return htmlHelper.ValidationMessage(tag, new { @class = "text-danger" });
}
public static MvcHtmlString MyValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
    return htmlHelper.ValidationMessageFor(expression, "", new { @class = "text-danger" });
}

并像使用@Html.MyValidationMessageFor(x => x.Street)

您可以轻松地调整它以检查元数据是否为空,如果是则不返回任何内容。

于 2016-08-22T13:33:05.737 回答