3

I need to create a page that has the equivalent to button click event in ASP.NET.

On my page when the user clicks a button I need to process some information and if an error occured then display an Error page, but if it was successful I need to display a successful page. I'm new at MVC and I'm not sure how to go about this...

This is what I've came up with so far (don't know if this will even work), I would create an ActionResult function to process the information then have the function decide which page should be displayed...

'//Foo page
Function Foo(Byval param1 as String, Byval param2 as String) As ActionResult
    Return View()
End Function

Function FooProcess(Byval param1 as String, Byval param2 as String) As ActionResult
    '//Look up information and process
    '//bSuccess = process(param1, param2)

    '//If bSuccess Then
    '//  redirect to successful page
    '//else
    '//  redirect to error page
    '//end if
End Function

Function FooSuccessful() As ActionResult
    Return View()
End Function

Function FooError(ByVal msg As String) As ActionResult
    Return View()
End Function
2
  • A regular button click event, no submit form button click.
    – Zach L
    Mar 15, 2011 at 21:18
  • My button will need to reload information about the page by looking it up in Database then process it. If everything looks good then it will need to display a successful page otherwise an error page. Robert posted a link to a good article that appears to have the solution I'm looking for!
    – Zach L
    Mar 15, 2011 at 21:31

3 Answers 3

2

you need to use [AcceptVerbs(HttpVerbs.Post)] and [AcceptVerbs(HttpVerbs.Get)] attributes to distinguish between normal and posted back page as for example here:

http://blog.jorritsalverda.nl/2010/03/10/maintainable-mvc-post-redirect-get-pattern/

2

I'm not sure how this will look in VB, but in C# (and in the spirit of MVC) you will need 3 things:

A Model:

public class SomeModel
{
    [DisplayName="Param One"]
    public String ParamOne{get; set;}

    [DisplayName="Param Two"]
    public String ParamTwo{get; set;}
}

A View:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SomeModel>" %>

<asp:Content ID="SomeID" ContentPlaceHolderID="TitleContent" runat="server">
    A title for your page
</asp:Content>
<asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
        <%        
        using (Html.BeginForm("Process", "SomeModel", returnURL))
        {%>
            <%= Html.LabelFor(m => m.ParamOne)%>:
            <%= Html.TextBoxFor(m => m.ParamOne)%>

            <%= Html.LabelFor(m => m.ParamTwo)%>:
            <%= Html.TextBoxFor(m => m.ParamTwo)%>

            <%--- A button ---%>
            <input type="submit" value="Press Me" />
        <% 
        } %>

        <%--- Display Errors ---%>
        <%= Html.ValidationSummary()%>


</asp:Content>

A Controller:

public class SomeModelController:Controller
{
     [HttpPost]
     public ActionResult Process(SomeModel model)
     {
         Validate(model);
         return View(model);
     }

     private bool Validate(SomeModel model)
     {
         if(/*both params are valid*/)
         {
             return true;
         }
         else
         {
             ModelState.AddError("error", "Some error message");
             return false;
         }
     }
}

Note that in this case any validation errors would be shown on the same page as they were input. If you want to change that you will have to modify the controller and add more views:

[HttpPost]
public ActionResult Process(SomeModel model)
{
    if(ModelState.IsValid && Validate(model))
    {
        return RedirectToAction("Success", "SomeModel");
    }
    else
    {
        return RedirectToAction("Failure", "SomeModel");
    }
}

[HttpGet]
public ActionResult Success(SomeModel model)
{
    return View(model); // Shows the Success.aspx page
}

[HttpGet]
public ActionResult Failure(SomeModel model)
{
    return View(model); // Shows the Failure.aspx page
}

Like I said, this is in C# but it shouldn't be that difficult to translate into VB... additionally this is just a general approach to the problem, you may have to tweak a few things to actually get it to work properly. The thing to note here is that the MVC pattern may seem a little cumbersome in the beginning, i.e. for a simple button you have to write A LOT of code, but it pays off when you have a complex application.

0

In the ASP.Net MVC world you would typically do something like this...

Note... that this would require 3 views Foo.aspx, FooPass.aspx, and FooFail.aspx and they would all take the Model MyModel

Another Note... You could also use your string parameters as you have in your sample. But this method allows for declarative validation with the Data Annoatations.

From here you could auto generate your views where Foo.aspx is an Edit view and both FooPass and FooFail are Detail views.

-- Controller --

<HandleError()> _
Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Public Function Foo() As ActionResult

        Dim model = New MyModel

        Return View(model)
    End Function

    <HttpPost()>
    Public Function Foo(ByVal model As MyModel) As ActionResult

        If (Me.ModelState.IsValid) Then

            If DoProcess(model) Then
                Return View("FooPass", model)
            Else
                Return View("FooFail", model)
            End If
        Else
            Return View(model)
        End If

    End Function

    Private Function DoProcess(ByVal model As MyModel) As Boolean
        Throw New NotImplementedException()
    End Function

End Class

-- Model --

Public Class MyModel    
    Public Property Param1() As String    
    Public Property Param2() As String    
End Class
2
  • Since my Foo page isn't an input page my GET and POST Foo ActionResult run into an error because they need the exact same parameters. I know this sounds weird, but my Foo page needs to have param1 and param2 in order to build and I need those values again in the POST in order to process the information. How can I fill my model in the POST without an input form on my page?
    – Zach L
    Mar 15, 2011 at 21:39
  • The model properties will match the inputs from the post or even from the URL query string. The get and post objects dont have to match it is just the typically convention. Mar 16, 2011 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.