locked
Razor syntax to complete an MVC form. RRS feed

  • Question

  • User-1674022954 posted

    Hi. I'm very new to using Razor with C# and am conducting this project in order to try to better my understanding of it.

    What this application is supposed to do, once completed, is ask the user to input three integers and then print out the sum of those integers. Right now, I have the basic frame of the View and Controller set up. (There is no Model.) The controller is set up to use an HTTP-Post protocol in order to send information to the HTML form.

    What I'm struggling with, is the code needed to communicate the data directly to the form, as well as whatever parameters are needed so that ASP.net will ignore the presence of two identically-named controller actions (which I'm told it should be able to do once the Razor syntax is set up properly). Any guidance here would be very helpful.

    Controller:

    public ActionResult Index(int firstInt = 0, int secondInt = 0, int thirdInt = 0)
            {
                return View();
            }
    
    [HttpPost]
    public ActionResult Index(int firstInt = 0, int secondInt = 0, int thirdInt = 0)
            {
                int sum = firstInt + secondInt + thirdInt;
                ViewBag.result = sum;
            }

    Index View:

    <form action="" method="post">
        <table>
        <tr><td>Enter the 1st Number: <input id="firstInt" name="firstInt" type="text" value="0" /></td></tr>
        <tr><td>Enter the 2nd Number: <input id="secondInt" name="secondInt" type="text" value="0" /></td></tr>
        <tr><td>Enter the 3rd Number: <input id="thirdInt" name="thirdInt" type="text" value="0" /></td></tr>
        <tr>
        <td><input id="Submit" type="submit" value="submit" /><input id="Reset" type="reset" value="reset" /></td>
        </tr>
        <tr>
        <td>Sum =  @ViewBag.result</td>
        </tr>
        </table>
    </form>

    Wednesday, October 11, 2017 11:40 PM

All replies

  • User347430248 posted

    Hi The Rarispy,

    you can refer example below may help you.

    model:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace math123.Models
    {
        public class Manish
        {
            public int sum(int a, int b)
            {
                int c = a + b;
                return c;
            } 
            public int sub(int a, int b)
            {
                int c = a + b;
                return c;
            }
            public int mul(int a, int b)
            {
                int c = a * b;
                return c;
            }
            public int div(int a, int b)
            {
                int c = a / b;
                return c;
            }
        }
    }

    controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using math123.Models;
    namespace math123.Controllers
    {
        public class manuController : Controller
        {
            //
            // GET: /manu/
            public ActionResult Index()
            {
                Manish man = new Manish();
                man.sum(10,20);
                man.sub(30, 10);
                man.mul(10, 50);
                man.div(100, 5);
                return View(man);
            }
        }
    }

    view:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<math123.Models.Manish>" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox2.Text = Model.sum(10, 20).ToString();
            TextBox3.Text = Model.sub(30, 10).ToString();
            TextBox4.Text = Model.mul(10, 50).ToString();
            TextBox5.Text = Model.div(100, 5).ToString();
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Index</title>
    </head>
    <body bgcolor="#008040">
        <form id="form1" runat="server">
        <div style="background-color: #66CCFF">
        Sum of Number
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <br />
            Sub of Number
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <br />
            Mul of Number
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            <br />
            Div of Number
            <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
            <br />
        </div>
        </form>
    </body>
    </html>

    Reference:

    Arithmetic operation in ASP.NET MVC

    Regards

    Deepak

    Thursday, October 12, 2017 5:43 AM
  • User-1674022954 posted

    Thank you for your suggestion, Deepak Panchal. I don't think I can use it though, because the thing I wanted to accomplish with this project was to learn Razor syntax. It's a requirement of the prompt I took that I use Razor syntax and the [HttpPost] method. But thank you anyway.

    Thursday, October 12, 2017 1:02 PM
  • User475983607 posted

    thing I wanted to accomplish with this project was to learn Razor syntax. It's a requirement of the prompt I took that I use Razor syntax and the [HttpPost] method. But thank you anyway.

    The problem is the example you selected is not good example of using Razor syntax.  See the following link to get started with Razor syntax.

    https://docs.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax-c

    Or if you are trying to learn the new Core Razor Pages, see the following link.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio

    Thursday, October 12, 2017 1:49 PM