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;
}
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>
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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.
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.
None
0 Points
2 Posts
Razor syntax to complete an MVC form.
Oct 11, 2017 11:40 PM|The Rarispy|LINK
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:
Index View:
Contributor
2990 Points
1210 Posts
Re: Razor syntax to complete an MVC form.
Oct 12, 2017 05:43 AM|Deepak Panchal|LINK
Hi The Rarispy,
you can refer example below may help you.
model:
controller:
view:
Reference:
Arithmetic operation in ASP.NET MVC
Regards
Deepak
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
2 Posts
Re: Razor syntax to complete an MVC form.
Oct 12, 2017 01:02 PM|The Rarispy|LINK
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.
All-Star
52221 Points
23292 Posts
Re: Razor syntax to complete an MVC form.
Oct 12, 2017 01:49 PM|mgebhard|LINK
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