Click Button Click event equivalent MVC

I need to create a page with the equivalent of a button event in ASP.NET.

On my page, when the user clicks the button, I need to process some information, and if an error occurs, display the error page, but if it was successful, I need to display the successful page. I'm new to MVC and I'm not sure how to do this ...

This is what I have come to so far (I don’t know if this will work), I would create an ActionResult function to process the information, after which the function decided 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 
+3
source share
3 answers

you need to use the attributes [AcceptVerbs (HttpVerbs.Post)] and [AcceptVerbs (HttpVerbs.Get)] to distinguish between the normal and backward page, for example, here:

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

+2
source

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

Model:

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

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> 

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

Please note that in this case, any validation errors will appear on the same page on which they were entered. If you want to change this, you will have to change 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 } 

As I said, this is in C #, but it’s not so difficult to translate to VB ... In addition, this is just a general approach to the problem, you may have to tweak a few things to actually work properly. It should be noted here that the MVC pattern may seem a little cumbersome at the beginning, i.e. For a simple button, you need to write A LOT code, but it pays off when you have a complex application.

+2
source

In the ASP.Net MVC world, you usually do something like this ...

Note ... that this will require 3 types of Foo.aspx, FooPass.aspx and FooFail.aspx, and they will all take Model MyModel

Another note .... You can also use your string parameters, as in your example. But this method allows declarative validation using annoatations data.

Here you can automatically generate your views, where Foo.aspx is the editing view, and FooPass and FooFail are the detailed 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
source

Source: https://habr.com/ru/post/1385878/


All Articles