Hi,
I referred this link (https://www.aspsnippets.com/Articles/Pass-Send-DataSet-DataTable-from-Controller-to-View-in-ASPNet-MVC.aspx) and passing data from Controller, but my project contains multiples Tables and I need to pass the data from Model->Controller->View.I am facing error while doing this. Kindly check and provide the solution for my issue. Code snap are attached below.
Error While Running the View
Server Error in '/' Application.
The model item passed into the dictionary is of type 'MyClassModel.Models.ItrackDD', but this dictionary requires a model item of type System.Collections.Generic.IEnumerable`1[MyClassModel.Models.MyClass]'.
<--Model--> (Data Representation)
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyClassModel.Models
{
public class ItrackDD
{
public List<int> Colors_ID { get; set; }
public List<string> ColorsInfo { get; set; }
public List<int> Completexity_code { get; set; }
public List<string> Completexity_name { get; set; }
public List<int> DeptCompletexity_code { get; set; }
public List<string> DeptCompletexity_name { get; set; }
}
}
<--Model--> (Business Logics)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace MyClassModel.Models
{
public class MyClassBL
{
string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public DataSet details()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(conn))
{
SqlCommand cmd = new SqlCommand("ItrackDropdown", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
return ds;
}
}
}
<--Controller-->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyClassModel.Models;
using System.Data;
namespace MyClassModel.Controllers
{
public class Home : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
MyClassBL obj = new MyClassBL();
DataSet ds=obj.details();
return View(ds);
}
}
}
<--View-->
@model IEnumerable<MyClass.Models.MyClass>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
@foreach (var item1 in @Model.Select(x => x.Colors_ID))
{
<tr>
<td>
@item1
</td>
</tr>
}
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
</body>
</html>