【问题标题】:Passing Multiple Dataset to View [MVC]将多个数据集传递给视图 [MVC]
【发布时间】: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


    【解决方案1】:

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

    a) 型号名称,

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

    public class ItrackDD
    

    应该是

    public class MyClass 
    

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

    模型ItrackDD的所有成员都是list,你确定吗?

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

    return View(newobj);
    

    但在 view 中,它需要一个字典,如下所示:

    @model IEnumerable<MyClass.Models.MyClass>
    

    【讨论】:

    • 1 a) 现在我将类名更正为 Myclass。实际上,我更改了用于发布堆栈溢出问题的代码。 B. 我没有使用实体框架。我认为这是不需要的。是的,模型类中的所有成员都是列表。 2. 是的,但是我要返回类的列表集合对象,对吗?如果我错了,请纠正我。我需要知道为什么会出现错误的主要原因。
    猜你喜欢
    • 2011-07-26
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多