我正在尝试使用ASP.NET MVC构建通用表单提交系统。我想尽可能简单地创建具有表单视图和“成功”视图的表单。使用WebForms方法,这很简单,可以使用模板或多视图完成。有了MVC,我有点卡住了。
以下是我要模仿的内容:
<% if (formNotSubmitted) { %>
<% Html.BeginForm(...); %>
<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<input id='submit' type='submit' value='Submit' />
<%= Html.ValidationSummary %>
<% Html.EndForm(); %>
<% } else { %>
<p>Thank you!</p>
<p><img src='thanks.jpg' /></p>
<p>Other items here maybe.</p>
<% } %>
理想情况下,我想使用Ajax,但也可以使用直接POST。我还想以某种方式包装它以避免使用“if..else”代码。使其比典型的ASP.NET MVC Ajax形式更难的是控制器将不知道成功消息/内容应该是什么。我见过的大多数演示都让控制器发回一条消息,但我在视图中需要这些代码。
感谢任何指导。
答案 0 :(得分:2)
你可以使用Controller
[AcceptVerbs("GET")]
public ActionResult Signup( )
{
// somecode that builds an html string
ViewData["form"] = htmlStringYouBuilt;
}
[AcceptVerbs("POST")]
public ActionResult Login( string username, string password )
{
// etc
}
then in the view
<%= ViewData["form"] %>
我已经成功地使用了这种技术。