小编joh*_*doe的帖子

如何在验证集合asp.net mvc中添加验证错误?

在我的控制器的动作中,我有以下代码:

public ActionResult GridAction(string id)
{
    if (String.IsNullOrEmpty(id)) 
    {
        // add errors to the errors collection and then return the view saying that you cannot select the dropdownlist value with the "Please Select" option
    }

    return View(); 
}
Run Code Online (Sandbox Code Playgroud)

更新:

if (String.IsNullOrEmpty(id))
{
    // add error 
    ModelState.AddModelError("GridActionDropDownList", "Please select an option");
    return RedirectToAction("Orders"); 
}
Run Code Online (Sandbox Code Playgroud)

更新2:

这是我更新的代码:

@Html.DropDownListFor(x => x.SelectedGridAction, Model.GridActions,"Please Select") 
@Html.ValidationMessageFor(x => x.SelectedGridAction)  
Run Code Online (Sandbox Code Playgroud)

该模型如下所示:

public class MyInvoicesViewModel
{

    private List<SelectListItem> _gridActions;

    public int CurrentGridAction { get; set; }

    [Required(ErrorMessage = …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-3

16
推荐指数
2
解决办法
5万
查看次数

标签 统计

asp.net-mvc ×1

asp.net-mvc-3 ×1