2013-07-26 23 views
0

我refferd这个帖子article stacoverflowsecond article sackoverflow不需要radioButtolListFor帮手 - MVC

和我使用RadioButtonListFor

public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(
     this HtmlHelper<TModel> htmlHelper, 
     Expression<Func<TModel, TProperty>> expression, 
     IEnumerable<SelectListItem> listOfValues, 
     IDictionary<string, object> radioHtmlAttributes = null, 
     string ulClass = null) 
     { 
      ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 

      if (radioHtmlAttributes == null) 
       radioHtmlAttributes = new RouteValueDictionary(); 

      TagBuilder ulTag = new TagBuilder("ul"); 
      if (!String.IsNullOrEmpty(ulClass)) 
       ulTag.MergeAttribute("class", ulClass); 

      if (listOfValues != null) 
      { 
       // Create a radio button for each item in the list 
       foreach (SelectListItem item in listOfValues) 
       { 

        // Generate an id to be given to the radio button field 
        var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value); 

        if (!radioHtmlAttributes.ContainsKey("id")) 
         radioHtmlAttributes.Add("id", id); 
        else 
         radioHtmlAttributes["id"] = id; 

        // Create and populate a radio button using the existing html helpers 
        var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text)); 
        var radio = htmlHelper.RadioButtonFor(expression, item.Value, radioHtmlAttributes).ToHtmlString(); 

        // Create the html string that will be returned to the client 
        // e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label> 
        ulTag.InnerHtml += string.Format("<li>{0}{1}</li>", radio, label); 
       } 
      } 

      return MvcHtmlString.Create(ulTag.ToString(TagRenderMode.Normal)); 
     } 

我的问题是,单选按钮是必需的,当我不检查一个单选按钮我有一个错误该字段是强制性的,我该如何禁用? (在我看来,型号我不使用dataAnnotation必填)

这是我的视图模型的一部分:

public class RegistrationViewModel 
    { 
      #region country 
      public string Country { get; set; } 
      private string CountryLabel { get; set; } 
      public ConfigurationParamValue CountryParam { get; set; } 
      #endregion 

      #region civilty 
      public int Civility { get; set; } 
      public ConfigurationParamValue CivilityParam { get; set; } 
      public string CivilityLabel { get; set; } 
      public List<Civility> ListCivilitys { get; set; } 
      #endregion 

} 

,这是我的观点的一部分:

<div id="city"> 
      <div class="editor-label" style="@visibleCivility"> 
        @Html.GetResource(Model.Category,Model.IsLocal,Constantes.CivilityLabel) 
       <div style="@mandatoryCivility">*</div> 
      </div> 
      <div class="editor-field"> 
       @Html.RadioButtonListFor(m => m.Civility, new SelectList(Model.ListCivilitys,"ID","Name")) 
       @Html.ValidationMessageFor(model => model.Civility) 
      </div> 
     </div> 
+1

然而,您可以显示模型类的定义吗? – Andrei

+0

您可能有必填字段验证程序? –

+0

我编辑的问题添加一些详细的,我不使用所需的验证程序 – user1428798

回答

1

我不是肯定为什么,但看起来HtmlHelper.RadioButtonFor方法返回一个包含“data-val-required”属性的标签。 如果你根本不希望此字段都被验证,你可以只是这个元素禁用ClientValidation就像这个例子:

@{Html.EnableClientValidation(false);} 
@Html.RadioButtonFor(m => m.MyRadioButton) 
@{Html.EnableClientValidation(true);} 

但是,我有这个问题,想来应该是依赖于其他的要求标签。我用于解决这个问题的一个解决方案是创建一个正则表达式来删除我们代码中的“data-val-required”属性。 看起来是这样的:

var radioButton = htmlHelper.RadioButtonFor(*parameters*); 
var requiredAttribute = *check if required attribute was set manually*; 
if (requiredAttribute == null) 
{ 
    Regex regex = new Regex("(data-val-required=\"[a-zA-ZåäöÅÄÖ \\s \\. 0-9]*\")"); 
    radioButton = regex.Replace(radioButton, ""); 
} 

这解决了我的问题,简单的和通用的方式。