"Require" validation (data annotation) not working for select list
No error comes when we apply require attribute to Select list in mvc 6.
Posted issue at http://forums.asp.net/t/2056134.aspx?Select+list+require+validation+mvc+6+using+data+annotation
Copying from the forum:
How to apply validation to select list in mvc 6 ( i need to implement data annotation validation )
for exaple in model i have applied "Required" attribute to "year" filed.
for example : code
<select asp-for="Year">
<option selected value=""></option>
<option value="2015">2015</option>
<option value="2014">2014</option>
</select>
<span asp-validation-for="Year"></span>
Other fields validation are working fine but for select item it is not working at all. can you give me a live example ?
The [Required] attribute meant something different than its MSDN documentation in earlier versions of MVC. We have corrected that bug in MVC 6. Use [BindRequired] to ensure a struct field has a value.
In this case your model likely includes
class MyModel
{
...
[Required]
public int Year { get; set; }
}
Change that to
class MyModel
{
...
[BindRequired]
public int Year { get; set; }
}
[BindRequired] is in the Microsoft.AspNet.Mvc.ModelBinding namespace.
Please update your forum issue and close it if this answers your question.
Thanks Dougbu, But it seems [BindRequired] is not working with me .
[BindRequired]
public string Year { get; set; }
[Required(AllowEmptyStrings =true,ErrorMessage = "Text Box value Required")]
public string Name { get; set; }
Issue-
- Using BindRequired how we can set error message like Required?
- BindRequired itself not working, i can give you source code to you.
Do you have some documentation for this ?
@rahuldhariya01
- Overriding the error message is not currently supported.
- Which MVC Beta are you using? The source code might help me see the issue faster.
Do you have some documentation for this ?
[BindRequired] is described fairly completely as "model binding will add a ModelState error for properties with this attribute that are not present in a submission`. Other than that, not much documentation (yet).
@dougbu Appreciate your help.
PFA (https://drive.google.com/file/d/0B_t4iGqASEkeN0tCSlV2UDQ2b2M/view?usp=sharing)
- Validation to select list
- how to set selected value from server side (C#)
- BindRequired is not working.
Go to Home/index.
@Dougbu, any update on this issue ?
@rahuldhariya01 Your Beta 4 application shouldn't need any change from the original. I incorrectly thought the Year property was an int but it's string. [Required] does what you want for string properties in all versions of MVC 6, including Beta 4. ([BindRequired] is more important in Beta 5 and Beta 6 but is not relevant for your project.)
What problems remain when using [Required]?
FYI I took your sample and changed the HomeModel back (I assume) to
[Required]
public string Year { get; set; }
The generated HTML is now:
<select data-val="true" data-val-required="The Year field is required." id="Year" name="Year">
<option value="">Select Now </option>
<option>2016</option>
<option>2017</option>
</select>
<span class="field-validation-valid" data-valmsg-for="Year" data-valmsg-replace="true"></span>
And client-side validation means I get errors if I hit Submit without filling in the Year field.
Thanks for help.