Hi anjali600,
In RedirectToAction is HTTP request you can't pass list.
So passing List from Controller to another is not possible.
You need to create another class with a string property. Then Serialize the data and set it.
In other Controller Deserialize it and return in your model.
Check the below example.
Namespaces
using Newtonsoft.Json;
Model Class
BasicInfo
public class BasicInfo
{
public string Name { get; set; }
public int Age { get; set; }
}
PersonData
public class PersonData
{
public string Data { get; set; }
}
Controller
Home
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(BasicInfo basic)
{
List<BasicInfo> infos = new List<BasicInfo>();
infos.Add(basic);
infos.Add(basic);
PersonData personData = new PersonData();
personData.Data = JsonConvert.SerializeObject(infos);
return RedirectToAction("Index", "Fetch", personData);
}
}
Fetch
public class FetchController : Controller
{
// GET: Fetch
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Index(PersonData personData)
{
List<BasicInfo> basicInfos = JsonConvert.DeserializeObject<List<BasicInfo>>(personData.Data);
return View(basicInfos);
}
}
View
Home
@model Sample_116258.Models.BasicInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<h4>Basic Info</h4>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="Age" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Create" /></td>
</tr>
</table>
}
</body>
</html>
Fetch
@model List<Sample_116258.Models.BasicInfo>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@if (Model != null)
{
<table>
@foreach (var basicInfo in Model)
{
<tr>
<td>Name</td>
<td>@basicInfo.Name</td>
</tr>
<tr>
<td>Age</td>
<td>@basicInfo.Age</td>
</tr>
}
</table>
}
</body>
</html>
Screenshot