首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在视图中显示视图模型中的数据

在视图中显示视图模型中的数据
EN

Stack Overflow用户
提问于 2013-05-29 14:33:27
回答 2查看 393关注 0票数 1

我尝试了这段代码,但出现了如下错误:

代码语言:javascript
运行
复制
The model item passed into the dictionary is of type 
'System.Collections.Generic.List`1[XNet.Repository.Model.RoomType]', 
but this dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable`1[XNet.Repository.Model.EditRoomTypeViewModel]'.

我不知道,什么部分会出错。请帮帮忙。

我的服务

代码语言:javascript
运行
复制
public List<EditRoomTypeViewModel> GetViewRoom(int RoomTypeID)

{
    List<RoomType> roomTypes = (from d in _RoomTypeRepository.All()
                          select d).ToList();

    List<EditRoomTypeViewModel> editRoomTypeViewModel = new List<EditRoomTypeViewModel>();

    foreach (RoomType roomType in roomTypes)
    {
        editRoomTypeViewModel.Add(new EditRoomTypeViewModel
        {
            RoomTypeID = RoomTypeID,
            RoomTypeName = roomType.RoomtypeName,
            RoomTypeDescription = roomType.RoomTypeDescripton,
        });
    }

    return editRoomTypeViewModel;
}

我的控制器

代码语言:javascript
运行
复制
public ActionResult Room()
        {
            ViewBag.hotel = _hotelService.GetByID(2).HotelName;

            List<EditRoomTypeViewModel> editRoomTypeViewModel = _roomViewService.GetViewRoom(_HotelID);
            return View(editRoomTypeViewModel.FirstOrDefault());
        }

我的视图模型

代码语言:javascript
运行
复制
 public class EditRoomTypeViewModel
    {
        public int RoomTypeID { get; set; }
        public string RoomTypeName { get; set; }
        public string RoomTypeDescription { get; set; }

    }

我的观点

代码语言:javascript
运行
复制
@model IEnumerable<XNet.Repository.Model.EditRoomTypeViewModel>
@{
    ViewBag.Title = "Room";
}

<h2>Room</h2>
<div>
    @Html.Label("Hotel Name");
</div>

<div>
   @ViewBag.hotel
</div>

<table>
    @foreach (var a in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => a.RoomTypeName)
            </td>
            <td>
               <input style="width:100px;" type="button" title="EditRoomType" value="Edit"  onclick="location.href='@Url.Action("EditRoom", "Hotel", new { RoomTypeID = a.RoomTypeID})'" />
            </td>
        </tr>
    }
</table>

<input style="width:200px;" type="button" title="EditRoomType" value="New Room Type"  onclick="location.href='@Url.Action("NewRoom", "Hotel")    '" />
EN

回答 2

Stack Overflow用户

发布于 2013-05-29 14:50:27

我注意到您在控制器中只返回了一个editRoomTypeViewModel对象,但是在您的视图中,您将模型声明为IEnumerable<XNet.Repository.Model.EditRoomTypeViewModel>

另一点是,错误似乎与其他地方的ViewBag赋值有关,因为它包含thisdictionaryrequires a model item of type,并且可能唯一类型为dictionary的东西是ViewBag

票数 0
EN

Stack Overflow用户

发布于 2013-05-29 18:26:15

只需删除控制器操作中的.FirstOrDefault(),就可以了。

代码语言:javascript
运行
复制
public ActionResult Room()
{
    ViewBag.hotel = _hotelService.GetByID(2).HotelName;

    List<EditRoomTypeViewModel> editRoomTypeViewModel = _roomViewService.GetViewRoom(_HotelID);
    return View(editRoomTypeViewModel);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16807006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档