0

デフォルトのフォーム検証を備えたフォームには、たとえば次のコードがあります。

コード:

<div class="item">
    @Html.LabelFor(m => m.Contact.LastName, new { @class="l" } )
    <div class="input">@Html.EditorFor(m => m.Contact.LastName, new { @class="i" })</div>
    <div class="marker">@Html.ValidationMessageFor(model => model.Contact.LastName)</div>
    <div class="clear"></div>
</div>

出力:

<div class="item">
  <label class="l" for="Contact_LastName">Lastname</label>
  <div class="input">
    <input class="i" data-val="true" data-val-length="Required" data-val-length-max="70" data-val-length-min="2" data-val-required="Required" id="Contact_LastName" name="Contact.LastName" type="text" value="" />
  </div>
  <div class="marker">
    <span class="field-validation-valid" data-valmsg-for="Contact.LastName" data-valmsg-replace="true">
    </span>
  </div>
  <div class="clear"></div>
</div>

問題:

ここで、deマーカーに「*」(アスタリスク)を表示したいのですが、フィールドにエラーがあると消えるか(エラーメッセージが表示されます)、フィールドが正しく入力されると消えます。

状況:

したがって、状態1-姓:[.....] *

状態2([送信]をクリックした後)-姓:[.....]が必要

状態3(有効なエントリ)-姓:[doe ..]

これを達成するための最良の方法は何ですか?

4

1 に答える 1

2

ValidateMessageForクラス(MVC4)のオーバーロードを作成しました。

    public  static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, bool addRequiredFieldMark)
    {
        return ValidationMessageFor(htmlHelper, expression, null /* validationMessage */, new RouteValueDictionary(), addRequiredFieldMark: addRequiredFieldMark);
    }

    public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage, IDictionary<string, object> htmlAttributes, bool? addRequiredFieldMark=false)
    {
        return ValidationMessageHelper(htmlHelper,
                                       ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData),
                                       ExpressionHelper.GetExpressionText(expression),
                                       validationMessage,
                                       htmlAttributes,
                                       addRequiredFieldMark:addRequiredFieldMark);
    }

    private static MvcHtmlString ValidationMessageHelper(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression, string validationMessage, IDictionary<string, object> htmlAttributes, bool? addRequiredFieldMark = false)
    {
        string modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);

        //Changed (http://stackoverflow.com/questions/8380988/how-to-customize-html-validationmessagefor-in-asp-mvc).
        //FormContext formContext = htmlHelper.ViewContext.GetFormContextForClientValidation();
        FormContext formContext = htmlHelper.ViewContext.ClientValidationEnabled ? htmlHelper.ViewContext.FormContext : null;

        if (!htmlHelper.ViewData.ModelState.ContainsKey(modelName) && formContext == null)
        {
            return null;
        }

        ModelState modelState = htmlHelper.ViewData.ModelState[modelName];
        ModelErrorCollection modelErrors = (modelState == null) ? null : modelState.Errors;
        ModelError modelError = (((modelErrors == null) || (modelErrors.Count == 0)) ? null : modelErrors.FirstOrDefault(m => !String.IsNullOrEmpty(m.ErrorMessage)) ?? modelErrors[0]);

        if (modelError == null && formContext == null)
        {
            return null;
        }

        TagBuilder builder = new TagBuilder("span");
        builder.MergeAttributes(htmlAttributes);
        builder.AddCssClass((modelError != null) ? HtmlHelper.ValidationMessageCssClassName : HtmlHelper.ValidationMessageValidCssClassName);

        if (!String.IsNullOrEmpty(validationMessage))
        {
            builder.SetInnerText(validationMessage);
        }
        else if (modelError != null)
        {
            builder.SetInnerText(GetUserErrorMessageOrDefault(htmlHelper.ViewContext.HttpContext, modelError, modelState));
        }

        if (formContext != null)
        {
            bool replaceValidationMessageContents = String.IsNullOrEmpty(validationMessage);

            // Addition
            if (modelMetadata != null && modelMetadata.IsRequired && addRequiredFieldMark.HasValue && addRequiredFieldMark.Value)
                builder.SetInnerText("*");

            if (htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
            {
                builder.MergeAttribute("data-valmsg-for", modelName);
                builder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
            }
            else
            {
                FieldValidationMetadata fieldMetadata = ApplyFieldValidationMetadata(htmlHelper, modelMetadata, modelName);
                // rules will already have been written to the metadata object
                fieldMetadata.ReplaceValidationMessageContents = replaceValidationMessageContents; // only replace contents if no explicit message was specified

                // client validation always requires an ID
                builder.GenerateId(modelName + "_validationMessage");
                fieldMetadata.ValidationMessageId = builder.Attributes["id"];
            }
        }
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
    }

だからこれは追加です:

if (modelMetadata != null && modelMetadata.IsRequired && addRequiredFieldMark.HasValue && addRequiredFieldMark.Value)                 
builder.SetInnerText("*");

そして、このかみそり/ cshtmlコード:

<div class="item">
    @Html.LabelFor(m => m.Contact.LastName, new { @class="l" } )
    <div class="input">@Html.EditorFor(m => m.Contact.LastName, new { @class="i" })</div>
    <div class="marker">@Html.ValidationMessageFor(model => model.Contact.LastName,true)</div>
    <div class="clear"></div>
</div>

(本当のキーワードを気に)

于 2012-08-10T13:29:39.440 に答える