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; }

Test クラス内で直接ビューを変更します

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

クライアント側の検証が機能し始めます...

4

0 に答える 0