IFormCollection action parameter in ASP.NET Core 2|3
Action Parameter
An action method can take parameters. These parameters are, for example, submitted form values or query string parameters. There are essentially three ways by which we can get all the submitted values. Formcollection in .net core is the most fundamental concept to retrieve form parsed values from view to controller’s action.
What are IFormCollection Parameters
•Represents the parsed form values sent with the HttpRequest.
•A parameter of any of this type will contain the list of values submitted by an HTML form; It won’t be used in a GET request
IFormCollection Example in asp.net core 2|3
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace VideoFile.Models
{
public class Employee
{
public int id { get; set; }
public string name { get; set; }
public string address { get; set; }
}
}
Home Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VideoFile.Models;
namespace VideoFile.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IFormCollection obj)
{
Employee obj1 = new Employee
{
id = Convert.ToInt32(obj["id"].ToString()),
name = obj["name"].ToString(),
address = obj["address"].ToString()
};
return RedirectToAction("Show", obj1);
}
public IActionResult Show(Employee obj)
{
return View(obj);
}
}
}
@model VideoFile.Models.Employee @{ ViewData["Title"] = "Index"; }Index
@using (Html.BeginForm()) { @Html.TextBoxFor(x => x.id)
@Html.TextBoxFor(x => x.name)
@Html.TextBoxFor(x => x.address)
}
Index.cshtml
@model VideoFile.Models.Employee @{ ViewData["Title"] = "Show"; }Show
Employee
- @Html.DisplayNameFor(model => model.id)
- @Html.DisplayFor(model => model.id)
- @Html.DisplayNameFor(model => model.name)
- @Html.DisplayFor(model => model.name)
- @Html.DisplayNameFor(model => model.address)
- @Html.DisplayFor(model => model.address)