【发布时间】:2018-12-08 11:02:01
【问题描述】:
我是 MVC 新手,我引用了这个链接 (https://www.aspsnippets.com/Articles/Pass-Send-DataSet-DataTable-from-Controller-to-View-in-ASPNet-MVC.aspx) 并从 Controller 传递数据,但我的项目包含多个表,我需要从 Model->Controller->View 传递数据。 我在执行此操作时遇到错误。请检查并为我的问题提供解决方案。
运行应用程序时出错
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]'.
(数据表示)
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; }
}
}
(业务逻辑)
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;
}
}
}
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);
}
}
}
@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>
【问题讨论】:
标签: asp.net asp.net-mvc-3 dataset