Wednesday, June 2, 2021

MVC How to Transfer data from View to Controller

To transfer data from View to Controller, we use name parameters of the form controls. The name parameter of web form control is used to pass data from that control on view page to action method of controller. The name parameter of input tag is used to transfer data from view to controller in ASP.NET applications in different ways.
  1. As parameter of action method
  2. Request["key"] method
  3. FormCollection as parameter of action method
  4. Request.QueryString["key"] method
  5. Form["key"] method
  6. Using model of the view as action method parameter
  7. Explicit Model Binding using UpdateModel or TryUpdateModel method
Model Binding happens behind the scene. The view model data gets bound with the parameters of the action method. The model binding can happen in implicit and explicit manner.
Limitations:
  • These methods receive data as string and type casting is required. 
  • If the number of parameters or keys is high, it becomes uneasy. 
The following examples explain these facts. We create a HomeController class file. The code of this file is as follows.

HomeController.cs
using System;
using System.Web.Mvc;
namespace WebRequest.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        // Parameters to get data from view
        // Parameter method works with both get and post methods
        public int SumParam(string number1, string number2)
        {
            int n1 = Convert.ToInt32(number1);
            int n2 = Convert.ToInt32(number2);
            return n1 + n2;
        }
        // Request object to get data from view
        // Request works with both get and post methods
        public int SumReq()
        {
            int n1 = Convert.ToInt32(Request["number1"]);           
            int n2 = Convert.ToInt32(Request["number2"]);
            return n1 + n2;
        }
        // FormCollection object to get data from view
        // FormCollection works with only post method
        public int SumColl(FormCollection fcoll)
        {
            int n1 = Convert.ToInt32(fcoll["number1"]);
            int n2 = Convert.ToInt32(fcoll["number2"]);
            return n1 + n2;
        }
    }
}
The view page is Index.cshtml which code is given below.
Index.cshtml


    Request Method: get and post both
    <div style="background-color:cyan;width:200px">
        <form action="~/Home/SumReq" method="get">
            <input type="text" name="number1" value="" />
            <br />
            <input type="text" name="number2" value="" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </div>
    <br />
    Parameter Method: get and post both
    <div style="background-color:cyan;width:200px">
        <form action="~/Home/SumParam" method="get">
            <input type="text" name="number1" value="" />
            <br />
            <input type="text" name="number2" value="" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </div>
    <br />
    FormCollection Method: with post method only
    <div style="background-color:cyan;width:200px">
        <form action="~/Home/SumColl" method="post">
            <input type="text" name="number1" value="" />
            <br />
            <input type="text" name="number2" value="" />
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </div>
OUTPUT

Example2 To Use Name Parameter to send data from View to Controller
HomeController.cs code

using System.Web.Mvc;
namespace WebView2ControllerParam.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        // Action method parameter name must match the control name given in the form of view page. Name is case sensitive also
        public string GetFullname(string first, string last)
        {
            string fullname = first + " " + last;
            Response.Write(fullname);
            Response.Write("");
            return fullname;
        }
    }
}
Index.cshtml code:

<h2>Index</h2>
<form action="Home/GetFullname" method="post">
    <input type="text" name="first" value="" />
    <br />
    <input type="text" name="last" value="" />
    <br />
    <input type="submit" name="submit" value="Submit" />
</form>


No comments:

Post a Comment

Hot Topics