1
public class Encapsulated
{
    [Required]
    public string CategoryId { get; set; }
}


 public class Category
{
    public string ID { get; set; }
    public string CategoryName { get; set; }

}


  public class Test
{



    public Encapsulated encapsulated { get; set; }

    private IEnumerable<Category> categories;

    public IEnumerable<Category> Categories
    {
        get { return 
            new List<Category>{new Category{ID="1",CategoryName="abc"}}; }
        set { categories = value; }
    }
}

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "Post" }))
{



@Html.DropDownListFor(
x => x.encapsulated.CategoryId,
    new SelectList(Model.Categories, "ID", "CategoryName"),
"-- Please select a category --"
)
@Html.ValidationMessageFor(x => x.encapsulated.CategoryId)

<input type="submit" value="Submit Form" />
}}

为什么客户端验证不适用于 dropDownList。如果我放置

[Required]
public string CategoryId { get; set; }

直接在我的测试类中并将视图更改为

 @Html.DropDownListFor( x => x.CategoryId,
 new SelectList(Model.Categories, "ID", "CategoryName"), "-- Please select a category --" ) @Html.ValidationMessageFor(x => x.CategoryId)

客户端验证开始工作...

4

0 回答 0