在我的控制器的动作中,我有以下代码:
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)