[RESOLVED]Custom Radio button with Validation Message

Hi

i am using custom Radio buttion method and also validation message.
Validation is working fine but validation message is not displaying.

my custom method,

public
static
MvcHtmlString
RadioButtonForSelectList<TModel, TProperty>(

this
HtmlHelper<TModel>
htmlHelper,
Expression<Func<TModel,
TProperty>> expression,
IEnumerable<SelectListItem>
listOfValues)

        {

           var
metaData =
ModelMetadata.FromLambdaExpression(expression,
htmlHelper.ViewData);

           var
sb =
new
StringBuilder();

           if
(listOfValues !=
null)

            {

           foreach
(
SelectListItem
item
in
listOfValues)

                {

               var
id =
string.Format("{0}_{1}",
metaData.PropertyName, item.Value);

               //
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,
new
{ id = id }).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>

                    sb.AppendFormat("<div
class="RadioButton">{0}{1}</div>"
, radio,label);

                }

            }

          return
MvcHtmlString.Create(sb.ToString());

        }

My view part,

@Html.RadioButtonForSelectList(z=>@ques.Q_id,
new

SelectList(@ques.ddl_values,
"Domn_CD",
"Domn_desc")) 

@Html.ValidationMessage(@ques.Q_id.ToString())

 

Rendered part-

<div class="RadioButton">

<input name="ques.Q_id" class="input-validation-error" id="Q_id_ISACC" type="radio" data-val-required="The Q_id field is required." data-val="true" data-val-number="The field Q_id must be a number." value="ISACC"/> <label id="Q_id_ISACC"> </div>

<div class="RadioButton"> <input name="ques.Q_id" class="input-validation-error" id="Q_id_ISACC" type="radio" value="ISACC"/> <label id="Q_id_ISACC"> <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="32"/> </div>

i need validation on this,Valis is YES,NO type.
if its not checked anyone value,it should display error message.

how can i achieve? please help me on this

Thanks,

Sundar.

 

Search for iclientvalidatable in mvc

Hi imran,

i am new to mvc. i am not aware on that.Could you plz give me some simple sample code?

It will be very helpfull for me.

 

Thanks,

Sundar.

Hi Sundar,

Sundar1987

<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="32"/> </div>

The reason for that issue is that you specify the value for ValidationMessage method.

You need specify the name (ques.Q_id).

@Html.ValidationMessage("ques.Q_id")

Best Regards

Starain

Hi Starain,

Thanks for your reply.its working fine. So name is more important.

Thanks,

Sundar.

Leave a Reply