2016-03-26 50 views
0

是否有將屬性嵌入到已在模型中設置的html輸入標記的方法?將MVC Model屬性設置爲HTML屬性

代碼

[Required] 
[StringLength(20)] 
public string Username { get; set; } 

[Required] 
[StringLength(20)] 
[DataType(DataType.Password)] 
public string Password { get; set; } 

預計渲染輸出繼電器:

<input id="Username" name="Username" maxlength="20" type="text" required="required" /> 
<input id="Password" name="Password" maxlength="20" type="password" required="required" /> 

我不想JavaScript驗證我想HTML驗證

+2

我認爲唯一的辦法是讓一個HTML輔助生成的HTML標記,並查找這些屬性在做的。 – juunas

回答

0

是有一種方法,但你必須做的你自己定製

你可以看看ho的源代碼W¯¯他們實現了它使用HtmlHelper擴展

http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/System.Web.Mvc/Html/InputExtensions.cs

你將需要看看下面得到你想要的東西上面

引用對於文本輸入

MvcHtmlString TextBoxFor<TModel, TProperty>(...); 
兩個輸入

輸入密碼開關輸入

MvcHtmlString PasswordFor<TModel, TProperty>(...); 

他們主要使用以下助手構建標記

// Helper methods 

private static MvcHtmlString InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, string name, object value, bool useViewData, bool isChecked, bool setId, bool isExplicitValue, string format, IDictionary<string, object> htmlAttributes) 
{ 
    string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); 
    if (String.IsNullOrEmpty(fullName)) 
    { 
     throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name"); 
    } 

    TagBuilder tagBuilder = new TagBuilder("input"); 
    tagBuilder.MergeAttributes(htmlAttributes); 
    tagBuilder.MergeAttribute("type", HtmlHelper.GetInputTypeString(inputType)); 
    tagBuilder.MergeAttribute("name", fullName, true); 

    string valueParameter = htmlHelper.FormatValue(value, format); 
    bool usedModelState = false; 

    switch (inputType) 
    { 
     case InputType.CheckBox: 
      bool? modelStateWasChecked = htmlHelper.GetModelStateValue(fullName, typeof(bool)) as bool?; 
      if (modelStateWasChecked.HasValue) 
      { 
       isChecked = modelStateWasChecked.Value; 
       usedModelState = true; 
      } 
      goto case InputType.Radio; 
     case InputType.Radio: 
      if (!usedModelState) 
      { 
       string modelStateValue = htmlHelper.GetModelStateValue(fullName, typeof(string)) as string; 
       if (modelStateValue != null) 
       { 
        isChecked = String.Equals(modelStateValue, valueParameter, StringComparison.Ordinal); 
        usedModelState = true; 
       } 
      } 
      if (!usedModelState && useViewData) 
      { 
       isChecked = htmlHelper.EvalBoolean(fullName); 
      } 
      if (isChecked) 
      { 
       tagBuilder.MergeAttribute("checked", "checked"); 
      } 
      tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue); 
      break; 
     case InputType.Password: 
      if (value != null) 
      { 
       tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue); 
      } 
      break; 
     default: 
      string attemptedValue = (string)htmlHelper.GetModelStateValue(fullName, typeof(string)); 
      tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(fullName, format) : valueParameter), isExplicitValue); 
      break; 
    } 

    if (setId) 
    { 
     tagBuilder.GenerateId(fullName); 
    } 

    // If there are any errors for a named field, we add the css attribute. 
    ModelState modelState; 
    if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState)) 
    { 
     if (modelState.Errors.Count > 0) 
     { 
      tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName); 
     } 
    } 

    tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata)); 

    if (inputType == InputType.CheckBox) 
    { 
     // Render an additional <input type="hidden".../> for checkboxes. This 
     // addresses scenarios where unchecked checkboxes are not sent in the request. 
     // Sending a hidden input makes it possible to know that the checkbox was present 
     // on the page when the request was submitted. 
     StringBuilder inputItemBuilder = new StringBuilder(); 
     inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing)); 

     TagBuilder hiddenInput = new TagBuilder("input"); 
     hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden)); 
     hiddenInput.MergeAttribute("name", fullName); 
     hiddenInput.MergeAttribute("value", "false"); 
     inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing)); 
     return MvcHtmlString.Create(inputItemBuilder.ToString()); 
    } 

    return tagBuilder.ToMvcHtmlString(TagRenderMode.SelfClosing); 
}