Passing Multiple Dataset to View [MVC]


Passing Multiple Dataset to View [MVC]



I am new to MVC, 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.



Error While running the Application


Server Error in '/' Application.
The model item passed into the dictionary is of type 'MyClassModel.Models.MyClass', 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 Myclass
{
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()
{


MyClass newobj=new MyClass();
MyClassBL obj = new MyClassBL();
newobj.Colors_ID= obj.details().Tables[0].AsEnumerable().Select(x => x.Field<int>("Colors_ID")).ToList();
newobj.ColorsInfo = obj.details().Tables[0].AsEnumerable().Select(x => x.Field<string>("ColorsInfo")).ToList();
newobj.Completexity_code = obj.details().Tables[1].AsEnumerable().Select(x => x.Field<int>("Complexity_code")).ToList();
newobj.Completexity_name = obj.details().Tables[1].AsEnumerable().Select(x => x.Field<string>("Complexity_name")).ToList();
newobj.DeptCompletexity_code = obj.details().Tables[2].AsEnumerable().Select(x => x.Field<int>("Complexity_code")).ToList();
newobj.DeptCompletexity_name = obj.details().Tables[2].AsEnumerable().Select(x => x.Field<string>("Complexity_name")).ToList();
return View(newobj);
}

}
}



<--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>




1 Answer
1



Two suggestions for your references:
1) Model



a) Model name,



According to the controller, the Model name of the first one need update that the following line:


public class ItrackDD



should be


public class MyClass



b) Model Design should match the database. it will impact how to fix the error according to #2.



All of the members of the model ItrackDD are list, are you sure?



2) The error message should come from:
In the controller, it returned the newobj of type Myclass as in the following code line, which would be passed to the view


return View(newobj);



but in the view, it expect a dictionary as in this line:


@model IEnumerable<MyClass.Models.MyClass>





1 a) Now I corrected the class name as Myclass. Actually I changed the code for posting in stack overflow question purpose. B. I am not using entity framwork. I think this is not needed. Yes all members in model class are list. 2. Yes, but I am returning list collection objects of class right? Correct me if I am wrong. Main reason I need to know why the error is coming.
– Nachiappan R
2 days ago





Is my modification is correct
– Nachiappan R
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV