传递多个数据集以查看[MVC]

时间:2018-06-29 09:27:09

标签: asp.net asp.net-mvc-3 dataset

我是MVC的新手,我引用了此链接(https://www.aspsnippets.com/Articles/Pass-Send-DataSet-DataTable-from-Controller-to-View-in-ASPNet-MVC.aspx)并从Controller传递数据,但是我的项目包含多个Tables,我需要从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>

1 个答案:

答案 0 :(得分:0)

两条建议供您参考: 1)型号

a)型号名称

根据 controller ,第一个控制器的Model名称需要更新为以下行:

public class ItrackDD

应该是

public class MyClass 

b)模型设计应与数据库匹配。这将影响如何根据#2修复错误。

ItrackDD模型的所有成员都是列表,您确定吗?

2)错误消息应来自: 在 controller 中,它返回Myclass类型的newobj,如以下代码行中所示,该代码将传递给 view

return View(newobj);

但在视图中,它期望像这样的字典:

@model IEnumerable<MyClass.Models.MyClass>