开发者

MVC equivalent to ASP.NET Button Click Event

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 proce开发者_如何转开发ss 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


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/


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜