【发布时间】:2011-05-07 19:11:45
【问题描述】:
我有一个联系页面,这个页面应该显示一个表单或一个成功消息或一个失败消息,所以基本上是这样的:
@model MyApp.Models.ContactData
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
...Some static content...
If page was opened the first time
-> Render a form here
Else If form was posted and data successfully processed
-> Render a success message here
Else If form was posted but error occurred during processing
-> Render a failure message here
...Some static content...
</div>
我不知道使用 MVC 3 实现这一目标的最佳方法是什么。我是否创建三个完全独立的视图(这是我想避免的,因为静态内容对于所有三个视图都是相同的)?或者我可以创建三个局部视图,然后根据我可以放入模型类的附加标志来决定要渲染哪个局部视图?或者我可以以某种方式将部分视图从控制器动态注入到视图中?
我目前的控制器是这样的:
public class ContactController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(ContactData contactData)
{
if (ModelState.IsValid)
{
ContactService service = new ContactService();
bool result = service.Process(contactData);
return ?; // What do I return now? It must somehow depend on result.
}
else
return View(contactData));
}
}
我有一个与 ASP.NET WebForms 类似的页面和行为,解决方案是将三个可变标记块放入 asp:Panel 控件,然后从代码打开或关闭这些面板的 Visible 标志-在后面。我想我需要使用 ASP.NET MVC 的另一种方法来达到相同的目标。
最好的方法是什么?
提前感谢您的建议!
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-3