验证消息显示,当页面加载 [英] Validation messages are displayed when page load
问题描述
我有在ASP.NET MVC 2.0验证的一个问题。我用控制器相同的操作来执行用户的请求。结果
例如:
公众的ActionResult指数(ReportModel模型)
{
如果(model.IsInitialDisplay&安培;!&安培; ModelState.IsValid)
{
model.Result = service.GetResult(模型);
}
返回查看(模型);
}
在ReportModel,我定义了一个标志IsInitialDisplay以确定该页面是否在初始显示或不显示:
公共类ReportModel
{
[必需(的ErrorMessage =*)]
公共字符串标准{搞定;组; }
公共BOOL IsInitialDisplay {搞定;组; }
公共ReportResult结果{搞定;组; } 公共ReportModel()
{
IsInitialDisplay = TRUE;
}
}
而在视图中,我用下面的code:
<使用%(Html.BeginForm())
{%GT;
<表>
&所述; TR>
<第i个
标准:
< /第i
&所述; TD>
<%= Html.TextBox(标准,)%GT;
<%= Html.ValidationMessage(标准)%GT;
< / TD>
< / TR>
< /表>
< BR />
<输入类型=提交值=提交/>
<%= Html.Hidden(IsInitialDisplay,FALSE)%GT;
<%}%GT;
当我想到,如果用户不输入任何的标准值,并点击提交按钮,将显示验证错误消息。结果
但总是显示在初始页面加载验证错误消息,我不知道如何prevent呢?结果
有人知道吗?谢谢,
[更新] 结果
我有如下更新了我的行动方法和它的似乎是罚款:
公众的ActionResult指数(ReportModel模型)
{
//这里收集了一些公共数据... 如果(model.IsInitialDisplay)
{
ModelState.Clear();
}
否则,如果(ModelState.IsValid)
{
model.Result = service.GetResult(模型);
}
返回查看(模型);
}
显示在初始页面加载错误消息的原因是因为你的控制器动作发生 ReportModel
模型论据。当您第一次访问这个行动 /首页/指数
您传递任何参数,当默认的模型绑定试图绑定到一个 ReportModel
实例,它触发验证错误。
这是一个不好的做法是使用相同的动作渲染和处理表单提交,但如果你真的想这样做,你可以尝试这样的:
公众的ActionResult指数(布尔?isInitialDisplay)
{
如果(isInitialDisplay.HasValue&安培;&安培;!isInitialDisplay.Value)
{
VAR模型=新ReportModel();
的UpdateModel(模型);
如果(ModelState.IsValid)
{
model.Result = service.GetResult(模型);
}
返回查看(模型);
} //初始请求
返回查看(新ReportModel());
}
在这种情况下,你不再需要在你的模型也不它设置为true构造函数中的 IsInitialDisplay
属性。
这是说,这里的推荐方式:
公众的ActionResult指数()
{
VAR模型=新ReportModel();
返回查看(模型);
}[HttpPost]
公众的ActionResult指数(ReposrtModel模型)
{
如果(!ModelState.IsValid)
{
返回查看(模型);
}
model.Result = service.GetResult(模型);
返回查看(模型);
}
I have a problem with validation in ASP.NET MVC 2.0. I use the same Action in Controller to perform user request.
For example:
public ActionResult Index(ReportModel model)
{
if (!model.IsInitialDisplay && ModelState.IsValid)
{
model.Result = service.GetResult(model);
}
return View(model);
}
In the ReportModel, I define a flag IsInitialDisplay to determine whether the page is initial displayed or not:
public class ReportModel
{
[Required(ErrorMessage = "*")]
public string Criteria { get; set; }
public bool IsInitialDisplay { get; set; }
public ReportResult Result { get; set; }
public ReportModel()
{
IsInitialDisplay = true;
}
}
And in the View, I use the following code:
<% using (Html.BeginForm())
{ %>
<table>
<tr>
<th>
Criteria:
</th>
<td>
<%= Html.TextBox("Criteria", "") %>
<%= Html.ValidationMessage("Criteria") %>
</td>
</tr>
</table>
<br />
<input type="submit" value="Submit" />
<%= Html.Hidden("IsInitialDisplay", false) %>
<% } %>
As I expect, if users don't input any value for Criteria and click Submit button, the error message for validation will be displayed.
But the validation error message always displayed on initial page load, I don't know how to prevent it?
Does anyone know? Thanks,
[Updated]
I have updated my Action method as below and it's seem to be fine:
public ActionResult Index(ReportModel model)
{
// Collecting some commons data here...
if (model.IsInitialDisplay)
{
ModelState.Clear();
}
else if (ModelState.IsValid)
{
model.Result = service.GetResult(model);
}
return View(model);
}
The reason an error message is displayed on initial page load is because your controller action takes ReportModel
model as argument. When you first access this action with /Home/Index
you are passing no arguments and when the default model binder tries to bind to a ReportModel
instance it triggers validation errors.
It is a bad practice to use the same action for both rendering and handling the form submission but if you really want to do it you could try like this:
public ActionResult Index(bool? isInitialDisplay)
{
if (isInitialDisplay.HasValue && !isInitialDisplay.Value)
{
var model = new ReportModel();
UpdateModel(model);
if (ModelState.IsValid)
{
model.Result = service.GetResult(model);
}
return View(model);
}
// Initial request
return View(new ReportModel());
}
In this case you no longer need the IsInitialDisplay
property on your model nor the constructor which sets it to true.
This being said, here's the recommended way:
public ActionResult Index()
{
var model = new ReportModel();
return View(model);
}
[HttpPost]
public ActionResult Index(ReposrtModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
model.Result = service.GetResult(model);
return View(model);
}
这篇关于验证消息显示,当页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!