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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 如何在验证集合中添加验证错误asp.net mvc?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的控制器的动作中,我有以下代码
  1. public ActionResult GridAction(string id)
  2. {
  3. if (String.IsNullOrEmpty(id))
  4. {
  5. // add errors to the errors collection and then return the view saying that you cannot select the dropdownlist value with the "Please Select" option
  6. }
  7.  
  8. return View();
  9. }

更新:

  1. if (String.IsNullOrEmpty(id))
  2. {
  3. // add error
  4. ModelState.AddModelError("GridActionDropDownList","Please select an option");
  5. return RedirectToAction("Orders");
  6. }

更新2:

这是我更新的代码

  1. @Html.DropDownListFor(x => x.SelectedGridAction,Model.GridActions,"Please Select")
  2. @Html.ValidationMessageFor(x => x.SelectedGridAction)

该模型如下所示:

  1. public class MyInvoicesviewmodel
  2. {
  3.  
  4. private List<SelectListItem> _gridActions;
  5.  
  6. public int CurrentGridAction { get; set; }
  7.  
  8. [required(ErrorMessage = "Please select an option")]
  9. public string SelectedGridAction { get; set; }
  10.  
  11. public List<SelectListItem> GridActions
  12. {
  13. get
  14. {
  15. _gridActions = new List<SelectListItem>();
  16. _gridActions.Add(new SelectListItem() { Text = "Export to Excel",Value = "1" });
  17.  
  18. return _gridActions;
  19. }
  20. }
  21. }

这里是我的控制器动作:

  1. public ActionResult GridAction(string id)
  2. {
  3. if (String.IsNullOrEmpty(id))
  4. {
  5. // add error
  6. ModelState.AddModelError("SelectedGridAction","Please select an option");
  7. return RedirectToAction("Orders");
  8. }
  9.  
  10. return View();
  11. }

什么都没发生!我完全迷失在这一个!

更新3:

我现在使用以下代码,但仍然验证不会触发:

  1. public ActionResult GridAction(string id)
  2. {
  3. var myviewmodel= new Myviewmodel();
  4. myviewmodel.SelectedGridAction = id; // id is passed as null
  5.  
  6. if (!ModelState.IsValid)
  7. {
  8. return View("Orders");
  9. }

更新4:

  1. $("#linkGridAction").click(function () {
  2. alert('link grid action clicked');
  3.  
  4. $.get('GridAction/',{ SelectedGridAction: $("#SelectedGridAction").val() },function (result) {
  5. alert('success');
  6. });
  7. });

控制器如下所示:

  1. // Orderviewmodel has a property called SelectedGridAction.
  2. public ActionResult GridAction(Orderviewmodel orderviewmodel)
  3. {
  4. return View();
  5. }

更新5:验证没有触发:

  1. public ActionResult GridAction(Orderviewmodel orderviewmodel)
  2. {
  3. if (!ModelState.IsValid)
  4. {
  5. return View("Orders",orderviewmodel);
  6. }
  7. return View();
  8. }

解决方法

您可以使用视图模型:
  1. public class Myviewmodel
  2. {
  3. [required]
  4. public string Id { get; set; }
  5. }

接着:

  1. public ActionResult GridAction(Myviewmodel model)
  2. {
  3. if (ModelState.IsValid)
  4. {
  5. // the model is valid,the user has selected an id => use it
  6. return RedirectToAction("Success");
  7. }
  8. return View();
  9. }

更新:

在对我的答复的数百条评论之后,我觉得有必要提供一个充分的工作实例:

像往常一样,从视图模型开始:

  1. public class Myviewmodel
  2. {
  3. [required]
  4. public string SelectedItemId { get; set; }
  5.  
  6. public IEnumerable<SelectListItem> Items
  7. {
  8. get
  9. {
  10. // Dummy data
  11. return new SelectList(Enumerable.Range(1,10)
  12. .Select(i => new SelectListItem
  13. {
  14. Value = i.ToString(),Text = "item " + i
  15. }),"Value","Text");
  16. }
  17. }
  18. }

然后一个控制器:

  1. public class HomeController: Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. return View(new Myviewmodel());
  6. }
  7.  
  8. [HttpPost]
  9. public ActionResult Index(Myviewmodel model)
  10. {
  11. if (!ModelState.IsValid)
  12. {
  13. // The user didn't select any value => redisplay the form
  14. return View(model);
  15. }
  16. // TODO: do something with model.SelectedItemId
  17. return RedirectToAction("Success");
  18. }
  19. }

最后的看法:

  1. <% using (Html.BeginForm()) { %>
  2. <%= Html.DropDownListFor(
  3. x => x.SelectedItemId,Model.Items,"-- Select Item --"
  4. ) %>
  5. <%= Html.ValidationMessageFor(x => x.SelectedItemId) %>
  6. <input type="submit" value="OK" />
  7. <% } %>

猜你在找的asp.Net相关文章