views:

608

answers:

1

I am trying to build a generic form submission system using ASP.NET MVC. I'd like to make it as easy as possible to create forms with a form view and a "success" view. Using the WebForms method, this was easy and could be accomplished with templates or multiviews. With MVC, I'm a bit stuck.

Here's what I'd like to emulate:

<% 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>
<% } %>

Ideally, I'd like to use Ajax, but also have it work with a straight POST. I'd also like to wrap this somehow to avoid the "if..else" code. What makes this harder than a typical ASP.NET MVC Ajax form is that the controller won't know what the success message/content is supposed to be. Most demos I've seen have the controller send back a message, but I need that code in the view.

Any guidance is appreciated.

+1  A: 

You could it with the 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"] %>

I've been able to use this technique successfully.

BioBuckyBall
Unfortunately, this isn't going to work for me because the controller doesn't have any idea what should be displayed after being called. It simply records the data (and should report back any errors if found).
Brian Vallelunga