Hi rani,
Use TempData object to pass from one Controller to another.
Check this example. Now please take its reference and correct your code.
For using Entity Framework in ASP.Net Core refer below article.
Model
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Namespaces
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
Controller
Home
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
List<Customer> customers = (from customer in this.Context.Customers.Take(10)
select customer).ToList();
return View(customers);
}
[HttpPost]
public IActionResult Index(string country)
{
List<Customer> customers = (from customer in this.Context.Customers
.Where(x => x.Country == country).Take(10)
select customer).ToList();
TempData["Customers"] = JsonConvert.SerializeObject(customers);
TempData["Country"] = country;
return RedirectToAction("Index", "Detail");
}
}
Detail
public class DetailController : Controller
{
public IActionResult Index()
{
List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(TempData["Customers"].ToString());
return View(customers);
}
}
View
Home > Index
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using EF_Core_MVC.Models;
@model IEnumerable<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form asp-action="Index" asp-controller="Home">
<input type="text" name="country" value="USA" />
<input type="submit" value="Send" />
</form>
<hr />
<table cellpadding="0" cellspacing="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
</tr>
}
</table>
</body>
</html>
Detail > Index
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using EF_Core_MVC.Models;
@model IEnumerable<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h4> Customers in <b>@TempData["Country"]</b> Country</h4>
<table cellpadding="0" cellspacing="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
</tr>
}
</table>
</body>
</html>
Screenshot