开发者

ASP.net MVC2 validation for my Dropdown list boxes?

Can anyone give me a nice link to Implement ASP.NET MVC2 validation for my dropdownlist in my view?

My Dropdownlist in my view is something like this:

<%: Html.DropDownListFor(model => model.SelectedStudent, 
                         new SelectList(Model.StudentIDs, "ID", "Name"),
                         "Please select..", 
                         new { id="Stude开发者_高级运维nt", style = "width:190px;" })%>

and my validation message is:

  <%:Html.ValidationMessageFor(model => model.SelectedStudent) %> 

in my Model I have this validation:

[DisplayName("Student")]
[Required(ErrorMessage="Please Select StudentID.")]
public int Student{ get; set; }

But somehow it's not validating and I am not seeing validation message in my view.

Something causing problem with "Please select"? or please correct me if I am wrong..

Thanks


In your view, you'll want to put something like this:

<%= Html.ValidationMessageFor(model => model.DropDownListReference) %>

In your model, something like this

public class Whatever
{
    [Required(ErrorMessage = "Please select a Whatever!")]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public int DropDownListReference { get; set; }
}

This is assuming that you just want to validate that they've selected something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜