1

I'm refferd to this post article stackoverflow and this second article stackoverflow

and I'm using the 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));
        }

My problem is that the radio button is required and when I don't check a radio button I Have an error the field is mandatory, how can I disable this ?? (In my view model I don't use the dataAnnotation Required)

this a part of my viewModel:

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

}

and this is a part of my view:

<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>
quant
  • 2,184
  • 2
  • 19
  • 29
user1428798
  • 1,534
  • 3
  • 24
  • 50

1 Answers1

1

Im not sure why, but it seems the HtmlHelper.RadioButtonFor method returns a tag including the "data-val-required"-attribute. If you simply don't want this field to be validated at all you can disable ClientValidation for just this element like in this example:

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

However, i had this problem and wanted the requirement to be dependant on another tag. One solution that I used for this problem was to create a regex to remove the "data-val-required"-attribute in our code. Which look something like this:

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, "");
}

This solved my problem in a simple and generic way.

trexium
  • 11
  • 1
  • 1