Using Traditional Approach
In the traditional approach we use the request object of the HttpRequestBase class. The request object has view input field values in name/value pairs.
[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("Interest : " + simpleInteresrt);
return Content(sbInterest.ToString());
}
Using the FormCollection Object
We can also get post requested data by the 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("Interest : " + simpleInteresrt);
return Content(sbInterest.ToString());
}
Using the Parameters
We can pass all input field names as a parameter to the post action method. The input field name and parameter name should be the same. These parameters have input field values that were entered by the user. So we can access view input field values from these parameters. The input field takes a string value from the user so the parameter should be a string type. There is no need to define a parameter in any specific sequence.
[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("Interest : " + simpleInteresrt);
return Content(sbInterest.ToString());
}
Strongly typed model binding to view
We bind a model to the view; that is called strongly type model binding.
namespace CalculateSimpleInterest.Models
{
public class SimpleInterestModel
{
public decimal Amount { get; set; }
public decimal Rate { get; set; }
public int Year { get; set; }
}
}
[HttpPost]
public ActionResult CalculateSimpleInterestResult(SimpleInterestModel model)
{
decimal simpleInteresrt = (model.Amount*model.Year*model.Rate)/100;
StringBuilder sbInterest = new StringBuilder();
sbInterest.Append("Interest : " + simpleInteresrt);
return Content(sbInterest.ToString());
}
my thanks to:
http://www.codeproject.com/Articles/639709/Getting-Data-From-View-to-Controller-in-MVC
0 comments:
Post a Comment