namespace CLIck10.App_Code
{
public static class Glob
{
...
}
}
Right click on the .cs file in the App_Code folder and check its properties.
Make sure the "Build Action" is set to "Compile".
namespace CLIck10.App_Code
{
public static class Glob
{
...
}
}
Right click on the .cs file in the App_Code folder and check its properties.
Make sure the "Build Action" is set to "Compile".
CalculateSimpleInterest controller (CalculateSimpleInterestController.cs) that renders the view on the UI.public ActionResult SimpleInterest()
{
return View();
}
Create a view to get user input from the UI, so the code is:<fieldset>
<legend>Calculate Simple Interest</legend>
@using (Ajax.BeginForm("CalculateSimpleInterestResult","CalculateSimpleInterest",
new AjaxOptions { UpdateTargetId = "divInterestDeatils" }))
{
<div id="divInterestDeatils"></div>
<ol>
<li>
@Html.Label("Amount")
@Html.TextBox("txtAmount")
</li>
<li>
@Html.Label("Rate")
@Html.TextBox("txtRate")
</li>
<li>
@Html.Label("Year")
@Html.TextBox("txtYear")
</li>
</ol>
<button>Calculate</button>
}
</fieldset>
So now the screen is ready to get input and it shows it as:FormCollection ObjectHttpRequestBase
class. The request object has view input field values in name/value
pairs. When we create a submit button then the request type POST is
created and calls the POST method.[HttpPost]
public ActionResult CalculateSimpleInterestResult()
{
decimal principle = Convert.ToDecimal(Request["txtAmount"].ToString());
decimal rate = Convert.ToDecimal(Request["txtRate"].ToString());
int time = Convert.ToInt32(Request["txtYear"].ToString());
decimal simpleInteresrt = (principle*time*rate)/100;
StringBuilder sbInterest = new StringBuilder();
sbInterest.Append("<b>Amount :</b> " + principle+"<br/>");
sbInterest.Append("<b>Rate :</b> " + rate + "<br/>");
sbInterest.Append("<b>Time(year) :</b> " + time + "<br/>");
sbInterest.Append("<b>Interest :</b> " + simpleInteresrt);
return Content(sbInterest.ToString());
}
When it executes, we get simple interest as the result as in the following:FormCollection object. The FormCollection object also has requested data in the name/value collection as the Request object. To get data from the FormCollection object we need to pass it is as a parameter and it has all the input field data submitted on the form.[HttpPost]
public ActionResult CalculateSimpleInterestResult(FormCollection form)
{
decimal principle = Convert.ToDecimal(form["txtAmount"].ToString());
decimal rate = Convert.ToDecimal(form["txtRate"].ToString());
int time = Convert.ToInt32(form["txtYear"].ToString());
decimal simpleInteresrt = (principle*time*rate)/100;
StringBuilder sbInterest = new StringBuilder();
sbInterest.Append("<b>Amount :</b> " + principle+"<br/>");
sbInterest.Append("<b>Rate :</b> " + rate + "<br/>");
sbInterest.Append("<b>Time(year) :</b> " + time + "<br/>");
sbInterest.Append("<b>Interest :</b> " + simpleInteresrt);
return Content(sbInterest.ToString());
}
It also gives the same output as Figure 1.3 shows.[HttpPost]
public ActionResult CalculateSimpleInterestResult(string txtAmount, string txtRate, string txtYear)
{
decimal principle = Convert.ToDecimal(txtAmount);
decimal rate = Convert.ToDecimal(txtRate);
int time = Convert.ToInt32(txtYear);
decimal simpleInteresrt = (principle*time*rate)/100;
StringBuilder sbInterest = new StringBuilder();
sbInterest.Append("<b>Amount :</b> " + principle+"<br/>");
sbInterest.Append("<b>Rate :</b> " + rate + "<br/>");
sbInterest.Append("<b>Time(year) :</b> " + time + "<br/>");
sbInterest.Append("<b>Interest :</b> " + simpleInteresrt);
return Content(sbInterest.ToString());
}
It also gives the same output as Figure 1.3 shows.namespace CalculateSimpleInterest.Models
{
public class SimpleInterestModel
{
public decimal Amount { get; set; }
public decimal Rate { get; set; }
public int Year { get; set; }
}
}
Step 2: Create an action method that render a view on the UI.public ActionResult SimpleInterest()
{
SimpleInterestModel model = new SimpleInterestModel();
return View(model);
}
Step 3: Create a strongly typed view that has the same screen as in Figure 1.1.@model CalculateSimpleInterest.Models.SimpleInterestModel
@{
ViewBag.Title = "SimpleInterest";
}
<h2>Calulate Simple Interest</h2>@using (Ajax.BeginForm("CalculateSimpleInterestResult","CalculateSimpleInterest",
new AjaxOptions { UpdateTargetId = "divInterestDeatils" }))
{
<fieldset>
<legend>Calulate Simple Interest</legend><div id="div1"></div>
<div class="editor-label">
@Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Amount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Year)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Year)
</div>
<p>
<input type="submit" value="Calculate" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Step 4: Create an action method that handles the POST request and processes the data.[HttpPost]
public ActionResult CalculateSimpleInterestResult(SimpleInterestModel model)
{
decimal simpleInteresrt = (model.Amount*model.Year*model.Rate)/100;
StringBuilder sbInterest = new StringBuilder();
sbInterest.Append("<b>Amount :</b> " + model.Amount+"<br/>");
sbInterest.Append("<b>Rate :</b> " + model.Rate + "<br/>");
sbInterest.Append("<b>Time(year) :</b> " + model.Year + "<br/>");
sbInterest.Append("<b>Interest :</b> " + simpleInteresrt);
return Content(sbInterest.ToString());
}
It also gives the same output as Figure 1.3 shows.HttpContext
object which makes unit testing very difficult. Just think os how to
unit test the below ASP.NET code-behind. How do I create an HttpCcontext object, how do I simulate the sender and EventArgs objects of the button clicks, etc.public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
This is my first MVC application
</div>
</body>
</html>
public class DisplayTimeController : Controller
{
//
// GET: /DisplayTime/
public ActionResult Index()
{
ViewData["CurrentTime"] = DateTime.Now.ToString();
return View();
}
}
<%: tag in the ASPX page as shown in the below code snippet.<body>
<div>
<%: ViewData["CurrentTime"] %>
</div>
</body>
Customer.public class Customer
{
private string _Code;
private string _Name;
private double _Amount;
public string Code
{
set
{
_Code = value;
}
get
{
return _Code;
}
}
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public double Amount
{
set
{
_Amount = value;
}
get
{
return _Amount;
}
}
}
DisplayCustomer”.public class CustomerController : Controller
{
…..
….
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = 12;
objCustomer.CustomerCode = "1001";
objCustomer.Amount = 90.34;
return View("DisplayCustomer",objCustomer);
}
}
if condition which displays the customer as a privileged customer if above 100 and a normal customer if below 100.<body>
<div>
The customer id is <%= Model.Id %> <br />
The customer Code is <%= Model.CustomerCode %> <br />
<% if (Model.Amount > 100) {%>
This is a priveleged customer
<% } else{ %>
This is a normal customer
<%} %>
</div>
</body>
DisplayCustomer’.<form action="DisplayCustomer" method="post">
Enter customer id :- <input type="text" name="Id" /> <br />
Enter customer code :- <input type="text" name="CustomerCode" /><br />
Enter customer Amount :-<input type="text" name="Amount" /><br />
<input type="submit" value="Submit customer data" />
</form>
DisplayCustomer”. So we need to get the data from the HTML controls, flourish the object, and send the object to the view.DisplayCustomer which flourishes the customer object by collecting data from Request.Form and sends the object to the view DisplayCustomer.public class CustomerController : Controller
{
…..
….
[HttpPost]
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Id"].ToString());
objCustomer.CustomerCode = Request.Form["Id"].ToString();
objCustomer.Amount = Convert.ToDouble(Request.Form["Amount"].ToString()); ;
return View("DisplayCustomer", objCustomer);
}
}
DisplayCustomer” view.
So right click on the view folder and click Add view. You should see a
dropdown as shown in the below figure. Give a view name, check Create a
strongly typed view, and bind this view to the customer class using the
dropdown, as shown in the below figure.if condition which displays the customer as a privileged customer if above 100 and normal customer if below 100.<body>
<div>
The customer id is <%= Model.Id %> <br />
The customer Code is <%= Model.CustomerCode %> <br />
<% if (Model.Amount > 100) {%>
This is a priveleged customer
<% } else{ %>
This is a normal customer
<%} %>
</div>
</body>
<form action="DisplayCustomer" method="post">
Enter customer id :- <input type="text" name="Id" /> <br />
Enter customer code :- <input type="text" name="CustomerCode" /><br />
Enter customer Amount :-<input type="text" name="Amount" /><br />
<input type="submit" value="Submit customer data" />
</form>
public class CustomerController : Controller
{
…..
….
[HttpPost]
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Id"].ToString());
objCustomer.CustomerCode = Request.Form["Id"].ToString();
objCustomer.Amount = Convert.ToDouble(Request.Form["Amount"].ToString()); ;
return View("DisplayCustomer", objCustomer);
}
}
TextBox, Label, ListBox, etc., just by invoking the appropriate function.form tag for HTML we need to use “Html.BeginForm”. Shown below is the code snippet for that:<% using (Html.BeginForm("DisplayCustomer","Customer",FormMethod.Post))
{%>
-- HTML input fields will go here
<%} %>
The above code will generate the below HTML:<form action="DisplayCustomer" method="post">
…..
…..
</form>
The HTML helper “beginform” takes three input
parameters: action name (method inside the controller), controller name
(actual controller name), and HTTP posting methodology (POST or GET).TextBox”
function of the HTMLHelper class as shown in the below code. This way
you can create HTML controls using the HTMLHelper class functions.Enter customer id :- <%= Html.TextBox("Id",Model)%> <br />
The above code snippet will generate the below HTML code:Enter customer id :- <input type="text" name="Id" /> <br />
To create a data entry screen like the one shown below, we need to the use the below code snippet.<% using (Html.BeginForm("DisplayCustomer","Customer",FormMethod.Post))
{ %>
Enter customer id :- <%= Html.TextBox("Id",Model)%> <br />
Enter customer code :- <%= Html.TextBox("CustomerCode",Model) %><br />
Enter customer Amount :- <%= Html.TextBox("Amount",Model) %><br />
<input type="submit" value="Submit customer data" />
<%} %>
[HttpPost]
public ActionResult DisplayCustomer(Customer obj)
{
return View(obj);
}
Enjoy your output for different conditions of customer amounts entered.There are Different Types of SQL Joins which are used to query data from more than one database tables. In this article, you will learn a...