asp.net-mvc – 如何在验证集合中添加验证错误asp.net mvc?
发布时间:2020-05-23 18:55:54 所属栏目:asp.Net 来源:互联网
导读:在我的控制器的动作中,我有以下代码: public ActionResult GridAction(string id){ if (String.IsNullOrEmpty(id)) { // add errors to the errors collection and then return the view saying that you cannot s
|
在我的控制器的动作中,我有以下代码: 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();
}
更新: if (String.IsNullOrEmpty(id))
{
// add error
ModelState.AddModelError("GridActionDropDownList","Please select an option");
return RedirectToAction("Orders");
}
更新2: 这是我更新的代码: @Html.DropDownListFor(x => x.SelectedGridAction,Model.GridActions,"Please Select") @Html.ValidationMessageFor(x => x.SelectedGridAction) 该模型如下所示: public class MyInvoicesViewModel
{
private List<SelectListItem> _gridActions;
public int CurrentGridAction { get; set; }
[Required(ErrorMessage = "Please select an option")]
public string SelectedGridAction { get; set; }
public List<SelectListItem> GridActions
{
get
{
_gridActions = new List<SelectListItem>();
_gridActions.Add(new SelectListItem() { Text = "Export to Excel",Value = "1" });
return _gridActions;
}
}
}
这里是我的控制器动作: public ActionResult GridAction(string id)
{
if (String.IsNullOrEmpty(id))
{
// add error
ModelState.AddModelError("SelectedGridAction","Please select an option");
return RedirectToAction("Orders");
}
return View();
}
什么都没发生!我完全迷失在这一个! 更新3: 我现在使用以下代码,但仍然验证不会触发: public ActionResult GridAction(string id)
{
var myViewModel= new MyViewModel();
myViewModel.SelectedGridAction = id; // id is passed as null
if (!ModelState.IsValid)
{
return View("Orders");
}
更新4: $("#linkGridAction").click(function () {
alert('link grid action clicked');
$.get('GridAction/',{ SelectedGridAction: $("#SelectedGridAction").val() },function (result) {
alert('success');
});
});
控制器如下所示: // OrderViewModel has a property called SelectedGridAction.
public ActionResult GridAction(OrderViewModel orderViewModel)
{
return View();
}
更新5:验证没有触发: public ActionResult GridAction(OrderViewModel orderViewModel)
{
if (!ModelState.IsValid)
{
return View("Orders",orderViewModel);
}
return View();
}
解决方法您可以使用视图模型:public class MyViewModel
{
[Required]
public string Id { get; set; }
}
接着: public ActionResult GridAction(MyViewModel model)
{
if (ModelState.IsValid)
{
// the model is valid,the user has selected an id => use it
return RedirectToAction("Success");
}
return View();
}
更新: 在对我的答复的数百条评论之后,我觉得有必要提供一个充分的工作实例: 像往常一样,从视图模型开始: public class MyViewModel
{
[Required]
public string SelectedItemId { get; set; }
public IEnumerable<SelectListItem> Items
{
get
{
// Dummy data
return new SelectList(Enumerable.Range(1,10)
.Select(i => new SelectListItem
{
Value = i.ToString(),Text = "item " + i
}),"Value","Text");
}
}
}
然后一个控制器: public class HomeController: Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
// The user didn't select any value => redisplay the form
return View(model);
}
// TODO: do something with model.SelectedItemId
return RedirectToAction("Success");
}
}
最后的看法: <% using (Html.BeginForm()) { %>
<%= Html.DropDownListFor(
x => x.SelectedItemId,Model.Items,"-- Select Item --"
) %>
<%= Html.ValidationMessageFor(x => x.SelectedItemId) %>
<input type="submit" value="OK" />
<% } %> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 本地化 – MVC 3中DataAnnotations的默认错误消息的整个列表
- asp.net-mvc – 奇怪的MVC问题
- asp.net – 在Global.asax方法中将与Autofac注册的组件的相
- asp-classic – 超过响应缓冲区限制
- asp.net-mvc – ASP.NET MVC 3:如何强制ActionLink执行Htt
- Asp.net配合easyui实现返回json数据实例
- asp.net-mvc – 在FSharp.Core旁边找不到FSharp.Core.sigda
- asp.net-core – compilationOptions.emitEntryPoint是什么
- asp.net成员资格 – 在Application_AuthenticationRequest设
- asp.net – Web.config自定义错误模式冲突
推荐文章
站长推荐
- ASP.NET MVC:经过几分钟的缓慢访问,然后每次以下
- asp.net-mvc – HttpResponse.RemoveOutputCache
- asp.net-mvc-4 – 如何在Kendo UI Grid中扩展页面
- asp.net-mvc – 有一个最佳实践和建议替代会话变
- asp.net-mvc – web.config在哪里用于MVC应用程序
- asp.net-mvc – 数据注释真的是一个好主意验证吗
- asp.net – 在文本框中输入按键时避免发出哔声
- asp.net-mvc – 在MVC中结合JS / CSS的任何经验?
- asp.net-mvc – 用于Kendo网格模板中的循环
- ASP.NET Health Monitoring和ELMAH是否相互替代?
热点阅读
