来自渲染操作的 MVC Razor 表单验证
我是 MVC 新手,正在尝试实现我所期望的常见问题。我有一个简单的搜索表单,我想在网站的每个页面上实现它。我希望此部分维护自己的代码,这样我就不必在每个页面上重复它。
到目前为止,我已经能够通过在模板页面上调用渲染操作来做到这一点。渲染操作填充快速搜索表单。当我提交表单时,我可以验证表单,但是我还没有找到一种方法来重新显示带有验证信息的同一页面。我更喜欢一种只刷新表单区域的方法,但只要重新显示页面,我就会接受完整的回发。
模板渲染调用
@{Html.RenderAction("Display", "QuickSearch");}
动作控制器
[HttpPost]
public ActionResult Submit(QuickSearchModel qsModel)
{
if (!ModelState.IsValid)
{
return PartialView(qsModel);
}
//Perform redirect
}
[ChildActionOnly]
public ActionResult Display()
{
//populate model
return View(qsModel);
}
快速搜索视图
<div>
@using (Html.BeginForm("Submit", "QuickSearch"))
{
@Html.ValidationSummary(true)
@Html.LabelFor(m => m.Destination)@Html.EditorFor(m => m.Destination)@Html.ValidationMessageFor(m => m.Destination)<br />
@Html.LabelFor(m => m.ArrivalDate)@Html.EditorFor(m => m.ArrivalDate)@Html.ValidationMessageFor(m => m.ArrivalDate)
@Html.LabelFor(m => m.DepartureDate)@Html.EditorFor(m => m.DepartureDate)@Html.ValidationMessageFor(m => m.DepartureDate)<br />
@Html.LabelFor(m => m.Adults)@Html.DropDownListFor(model => model.Adults, new SelectList(Model.AdultsSelectOptions, "value", "text", Model.Adults))<br />
@Html.LabelFor(m => m.Children)@Html.DropDownListFor(model => model.Children, new SelectList(Model.ChildrenSelectOptions, "value", "text", Model.Children))<br />
<input id="qsSubmit" name="qsSubmit" type="submit" value="Submit" />
}
</div>
提前感谢您的帮助!
I am new to MVC and trying to implement what I would expect to be a common problem. I have a simple search form that I want to implement on each page of the site. I want this section to maintain its own code so that I don't have to duplicate it on each page.
So far I have been able to do this by calling a render action on the template page. The render action populates the quicksearch form. When I submit the form I am able to validate the form, however I have not found a way to redisplay the same page with the validation information. I would prefer a way that would just refresh the form area, but I would accept a full postback as long as the page is redisplayed.
Template Render Call
@{Html.RenderAction("Display", "QuickSearch");}
ActionController
[HttpPost]
public ActionResult Submit(QuickSearchModel qsModel)
{
if (!ModelState.IsValid)
{
return PartialView(qsModel);
}
//Perform redirect
}
[ChildActionOnly]
public ActionResult Display()
{
//populate model
return View(qsModel);
}
Quick Search View
<div>
@using (Html.BeginForm("Submit", "QuickSearch"))
{
@Html.ValidationSummary(true)
@Html.LabelFor(m => m.Destination)@Html.EditorFor(m => m.Destination)@Html.ValidationMessageFor(m => m.Destination)<br />
@Html.LabelFor(m => m.ArrivalDate)@Html.EditorFor(m => m.ArrivalDate)@Html.ValidationMessageFor(m => m.ArrivalDate)
@Html.LabelFor(m => m.DepartureDate)@Html.EditorFor(m => m.DepartureDate)@Html.ValidationMessageFor(m => m.DepartureDate)<br />
@Html.LabelFor(m => m.Adults)@Html.DropDownListFor(model => model.Adults, new SelectList(Model.AdultsSelectOptions, "value", "text", Model.Adults))<br />
@Html.LabelFor(m => m.Children)@Html.DropDownListFor(model => model.Children, new SelectList(Model.ChildrenSelectOptions, "value", "text", Model.Children))<br />
<input id="qsSubmit" name="qsSubmit" type="submit" value="Submit" />
}
</div>
Thanks in advance for any assistance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现您遇到以下问题:
能够以任何简单的方式“重新生成”此帖子;
证实?
考虑到所有这些挑战,我首先会认真考虑以 AJAX 风格制作这个搜索表单。如果它适合您,那将是更简单的解决方案。
如果 AJAX 不是一个选项,我会看到以下相应问题的解决方案:
I see that you have the following problems:
be able to "re-produce" this POST in any easy way;
validate?
Given all these challenges, I would first seriously consider making this search form in AJAX style. That would be much easier solution, if it fits you.
If AJAX is not an option I see following solutions to the respective questions: