控制器中的参考模型类,因此多个数据可以在一个视图中

时间:2017-10-05 10:50:49

标签: sql asp.net sql-server asp.net-mvc

我试图从多个SQL Server存储过程中获取数据,以便在1个视图中访问,这样我就可以运行比较并创建图形/网格等。

我让每个部分都自己工作,我现在正试图让他们一起工作。

我已经更改了我的控制器并将字段类型放入他们自己的类中,然后创建了一个“Ring of Rings”类,并将它们全部放入。

namespace APP.Models
{
    public class SP_RESULTS
    {
        public type Type { get; set; }
        public status Status { get; set; }
        public condition Condition { get; set; }
        public rooms Rooms { get; set; }
    }

    public class type
    { 
        [Key]
        public decimal type_id { get; set; }
        public string type_code { get; set; }
        public string type_name { get; set; }
    }

    public class status 
    {
        //status
        public decimal status_id { get; set; }
        public string status_code { get; set; }
        public string status_name { get; set; }
        public string rentable { get; set; }
    }

    public class condition 
    {
        //condition
        public decimal condition_id { get; set; }
        public string condition_code { get; set; }
        public string condition_name { get; set; }
        public string rentable { get; set; }
        public int colour_code { get; set; }
        public int service_order { get; set; }
    }

    public class rooms  
    {
        //rooms
        public decimal room_id { get; set; }
        public string room_no { get; set; }
        public decimal type_id { get; set; }
        public int floor { get; set; }
        public decimal status_id { get; set; }
        public decimal condition_id { get; set; }
    }
}

然后我修改了运行SQL Server存储过程的控制器的每个部分,使用正确的类名而不是“ring of ring”名称,IE:

var outputmodel4 = new List<rooms>();
var command4 = db.Database.Connection.CreateCommand();
command4.CommandText = "SELECT rooms.room_id , rooms.room_no , rooms.type_id , rooms.floor_no , rooms.status_id , rooms.condition_id FROM rooms";

using (var SPOutput4 = command4.ExecuteReader())
{
    foreach (var row in SPOutput4)
    {
        outputmodel4.Add(new rooms()
                {
                    room_id = (decimal)SPOutput4["room_id"],
                    room_no = (string)SPOutput4["room_no"],
                    type_id = (decimal)SPOutput4["type_id"],
                    floor_no = (int)SPOutput4["floor_no"],
                    status_id = (decimal)SPOutput4["status_id"],
                    condition_id = (decimal)SPOutput4["condition_id"],
                });
    }

    db.Database.Connection.Close();

    return View(outputmodel4);
}
...etc for other SQL stored procedures

..和我的观点相同

@model IEnumerable<app.Models.SP_RESULTS >

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Rooms.room_id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Rooms.room_no)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Rooms.room_id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Rooms.room_no)
        </td>
    </tr>
}

</table>
…etc for the other models

VBS中的一切看起来都很好(没有红色的摇摆),但是当我在浏览器中访问视图时,我收到一个错误:

  应用程序中的服务器错误。

     

传递到字典中的模型项的类型为'System.Collections.Generic.List 1[app.Models.rooms]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [app.Models.SP_RESULTS]'。

     

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

     

异常详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为'System.Collections.Generic.List 1[app.Models.rooms]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [app.Models.SP_RESULTS]'。

如果我回到我的“戒指戒指”模型,并将其更改为:

public class SP_RESULTS
{
    public IEnumerable<type> Type { get; set; }
    public IEnumerable<status> Status { get; set; }
    public IEnumerable<condition> Condition { get; set; }
    public IEnumerable<rooms> Rooms { get; set; }
}

并在我的控制器中更改以下行:

var outputmodel4 = new List<rooms>();
outputmodel4.Add(new rooms()

var outputmodel4 = new List<SP_RESULTS>();
outputmodel4.Add(new SP_RESULTS()

VBS告诉我那个

  

SP_RESULTS不包含room_id的定义

我尝试使用类名称为定义添加前缀,但没有运气。

我看过SO,fourms.asp.net和google ..无法看到解决方案(可能有,但我不确定我正在寻找什么解决方案......如果这样做有意义的话。)< / p>

有人能够告诉我我需要做些什么来让我的所有模型类在同一个视图中工作吗?

我现在道歉,因为我意识到这个问题可能已被问过几百万次,但我似乎无法理解跳出来说这就是这样做的。

1 个答案:

答案 0 :(得分:0)

对于其他寻找答案的人(以及未来的我),我通过推出这样的模型来完成这项工作:

namespace APP.Models
{
    public class SP_RESULTS
    {
        public type Type { get; set; }
        public status Status { get; set; }
        public condition Condition { get; set; }
        public rooms Rooms { get; set; }

        public class type
    { 
        [Key]
        public decimal type_id { get; set; }
        public string type_code { get; set; }
        public string type_name { get; set; }
    } 

    public class status 
    {
        //status
        public decimal status_id { get; set; }
        public string status_code { get; set; }
        public string status_name { get; set; }
        public string rentable { get; set; }
    }

    public class condition 
    {
        //condition
        public decimal condition_id { get; set; }
        public string condition_code { get; set; }
        public string condition_name { get; set; }
        public string rentable { get; set; }
        public int colour_code { get; set; }
        public int service_order { get; set; }
    }

    public class rooms  
    {
        //rooms
        public decimal room_id { get; set; }
        public string room_no { get; set; }
        public decimal type_id { get; set; }
        public int floor { get; set; }
        public decimal status_id { get; set; }
        public decimal condition_id { get; set; }
    }

  }

}

在控制器中为每个Store过程使用VIEWDATA:

        var outputmodel3 = new List<SP_RESULTS.conditions>();
        var command3 = db.Database.Connection.CreateCommand();
        command3.CommandText = "dbo.pr_PROCEDUREONE";
        command3.CommandType = System.Data.CommandType.StoredProcedure;

        using (var SPOutput3 = command3.ExecuteReader())
        {
            foreach (var row in SPOutput3)
            {
                outputmodel3.Add(new SP_RESULTS.conditions()
                {
                    condition_id = (decimal)SPOutput3["condition_id"],
                    condition_code = (string)SPOutput3["condition_code"],
                    condition_name = (string)SPOutput3["condition_name"],
                });
            }
            ViewData["CONDITIONSOutput"] = outputmodel3;
        }

        var outputmodel4 = new List<SP_RESULTS.rooms>();
        var command4 = db.Database.Connection.CreateCommand();
        command4.CommandText = "SELECT rooms.room_id , etc FROM rooms";

        using (var SPOutput4 = command4.ExecuteReader())
        {
            foreach (var row in SPOutput4)
            {
                outputmodel4.Add(new SP_RESULTS.rooms()
                {
                    room_id = (decimal)SPOutput4["room_id"],
                    room_no = (string)SPOutput4["room_no"],
                });
            }
            ViewData["ROOMSOutput"] = outputmodel4;
            db.Database.Connection.Close();
            return View();
        }

..然后将我的观点改为: -

table class="table">
<tr>
    <th>
       Condition ID
    </th>
    <th>
        Condition code
    </th>
    <th>
        Condition name
    </th>
    <th>
        is it rentable?
    </th>
    <th>
        Condition color code
    </th>
    <th>
        Condition service order
    </th>
    <th></th>
</tr>

@foreach (var item in ViewData["CONDITIONSOutput"] as IEnumerable<Happ.Models.SP_RESULTS.conditions>)
 {
        <tr>
            <td>
                @item.condition_id
            </td>
            <td>
                @item.condition_code
            </td>
            <td>
                @item.condition_name
            </td>
            <td>
                @item.rentable
            </td>
            <td>
                @item.colour_code
            </td>
            <td>
                @item.service_order
            </td>
        </tr>
    }

</table>

<table class="table">
    <tr>
        <th>
           Room ID
        </th>
        <th>
           Room No
        </th>
        <th>
           Rooms type_id
        </th>
        <th></th>
    </tr>

@foreach (var item in ViewData["ROOMSOutput"] as IEnumerable<APP.Models.SP_RESULTS.rooms>)
{
        <tr>
            <td>
                @item.room_id
            </td>
            <td>
                @item.room_no
            </td>
        </tr>
}

</table>