开发者

Proper way to handle button clicks in asp.net mvc?

开发者 https://www.devze.com 2023-01-21 11:52 出处:网络
I\'m making a webpage (http://example.com/calculation/). This site will do simple calculations. The page will present the user with two input fields in the form of text boxes (asp:TextBox). I\'m wonde

I'm making a webpage (http://example.com/calculation/). This site will do simple calculations. The page will present the user with two input fields in the form of text boxes (asp:TextBox). I'm wondering how do I go about handling clicking on the "Calc" button (asp:Button)?

Do I u开发者_开发百科se the controller for the page since I'm using MVC template? How should I organize my code?

I want to fetch the users input in the two text boxes and output the value in a "result" label.


The simplest clean way provides a Model class, a Controller and a View. Please look at the following example:

The Model:

public class CalculatorModel {
    public int Result { get; set; }
    public int FirstOperand { get; set; }
    public int SecondOperand { get; set; }
}

The Controller:

public class CalculatorController : Controller {
    [HttpGet]
    public ActionResult Sum() {
        CalculatorModel model = new CalculatorModel();
        //Return the result
        return View(model);
    }

    [HttpPost]
    public ActionResult Sum( CalculatorModel model ) {
        model.Result = model.FirstOperand + model.SecondOperand;
        //Return the result
        return View(model);
    }
}

The View:

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

    <% using ( Html.BeginForm("Sum", "Calculator", FormMethod.Post, new { id = "calcForm" }) ) { %>
       <table border="0" cellpadding="3" cellspacing="1" width="100%">
       <tr valign="top">
           <td>
              <%= Html.LabelFor(model => model.FirstOperand) %>
              <%= Html.TextBoxFor(model => model.FirstOperand) %>
           </td>
       </tr>
       <tr valign="top">
           <td>
              <%= Html.LabelFor(model => model.SecondOperand) %>
              <%= Html.TextBoxFor(model => model.SecondOperand) %>
           </td>
       </tr>
       </table>
        <div style="text-align:right;">
            <input type="submit" id="btnSum" value="Sum values" />
        </div>
    <% } %>

My advice is to follow some tutorial on ASP.NET MVC. You can find many with google. The ASP.NET MVC web site is a good place to start.

Hope it helps!


I believe your question is hinged on a common misunderstanding for new comers to the MVC design pattern. In the MVC design pattern how you organize your model, view, and controllers is a matter of preference.

That said, web frameworks like ASP.NET MVC suggest certain organizations because of their tendency to expose pattern implementation via a site's URLs. To illustrate out of the box ASP.NET MVC would create this route http://example.com/calculation/add for the Calculation controller's add action. As a developer you can override this behavior by creating custom routes, which means you should organize your models, views, and controllers in a way that makes logical sense to you.

Since by definition your site is just doing simple calculations I would recommend creating a single controller with a variety of actions: add, subtract, divide etc. Lorenzo provides the basis of how to get started in his answer.

0

精彩评论

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