MVC相当于ASP.NET Button Click Event

时间:2011-03-15 21:13:33

标签: asp.net-mvc

我需要创建一个与ASP.NET中的按钮单击事件等效的页面。

在我的页面上,当用户点击按钮时,我需要处理一些信息,如果发生错误,则显示错误页面,但如果成功,我需要显示一个成功的页面。我是MVC的新手,我不知道如何解决这个问题......

这是我到目前为止所提出的(不知道这是否会起作用),我会创建一个ActionResult函数来处理信息然后让函数决定应该显示哪个页面... < / p>

'//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

3 个答案:

答案 0 :(得分:2)

您需要使用[AcceptVerbs(HttpVerbs.Post)]和[AcceptVerbs(HttpVerbs.Get)]属性来区分正常页面和已发布的页面,例如:

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

答案 1 :(得分:2)

我不确定这在VB中看起来如何,但在C#中(并且在MVC的精神中)你将需要3件事:

模特:

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

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

观点:

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

控制器:

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

请注意,在这种情况下,任何验证错误都将显示在输入的同一页面上。如果要更改它,则必须修改控制器并添加更多视图:

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

就像我说的那样,这是在C#中,但是转换成VB并不是那么困难...另外这只是解决这个问题的一般方法,你可能需要调整一些东西才能实现它好好工作。这里要注意的是MVC模式在开始时看起来有点麻烦,即对于一个简单的按钮,你必须编写很多的代码,但是当你有一个复杂的应用程序时它会得到回报

答案 2 :(得分:0)

在ASP.Net MVC世界中,你通常会做这样的事情......

注意...... 这需要3个视图Foo.aspx,FooPass.aspx和FooFail.aspx,他们都会采用Model MyModel

另一个注意事项...... 您也可以使用样本中的字符串参数。但是这种方法允许使用Data Annoatations进行声明性验证。

从这里你可以自动生成你的视图,其中Foo.aspx是一个Edit视图,FooPass和FooFail都是Detail视图。

- 控制器 -

<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

- 模型 -

Public Class MyModel    
    Public Property Param1() As String    
    Public Property Param2() As String    
End Class