【问题标题】:For each loop with submitting form对于每个带有提交表单的循环
【发布时间】:2015-05-14 11:48:05
【问题描述】:

样板间:

public class Room
    {
        public int Id { get; set; }
        public string NumberRoom { get; set; }
        public double CostPerNight { get; set; }
        public virtual Category Category { get; set; }
}

我的视图模型代码

public class RoomModel
    {
        public IList<Room> Rooms { get; set; }
    }

我的剃刀代码:

@model hotel.Models.RoomModel

@using (Html.BeginForm("ComfortLevelView", "Category"))
        {
            for (int i = 0; i < Model.Rooms.Count(); i++)
             {
                     <table class="simple-little-table" cellspacing='0'>
                         <tr>
                             <td>@Html.DisplayFor(m => Model.Rooms[i].NumberRoom) </td>
                             <td>@Html.DisplayFor(m => Model.Rooms[i].Categoryid)</td>
                             <td>@Html.DisplayFor(m => Model.Rooms[i].NumberOfSeats) </td>
                             <td>
                                 @{ var result = Model.Rooms[i].CostPerNight * numberNights; }
                                 <p>@ViewBag.NumberNights ночей</p>:@result
                             </td>
                             <td>
                                 <input type="submit" id="submit" value="Booking" />
                             </td>
                         </tr>
                     </table>
                 </div>
             }
        }

控制器:

public ActionResult ComfortLevelView(int NumberNights, int CategoryId, int NumberPeoples ,DateTime SelectedDate)
        {
            IRoomService roomService = new RoomService();;
            return View(roomService.GetRoomsByCategory(CategoryId, SelectedDate, NumberNights, NumberPeoples));
        }

[HttpPost]
public ActionResult ComfortLevelView(RoomModel model)
{
    //
}

传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery`1[Hotel.BusinessObject.Room]”,但此字典需要“hotel.Models.RoomModel”类型的模型项.

【问题讨论】:

  • 您尚未解决上一个问题中的多个错误中的任何一个!您的查询会生成 Room 对象的集合,而不是 RoomModel 对象!
  • 这也不是 for-each 循环,它只是一个 for 循环
  • 上一题帮了很多忙,没意义!

标签: asp.net-mvc razor


【解决方案1】:

错误消息是不言自明的。你有这个观点

@model hotel.Models.RoomModel

但是由于控制器中的这行代码,您将System.Data.Entity.Infrastructure.DbQuery&lt;Hotel.BusinessObject.Room&gt; 的实例传递给您的视图

return View(roomService.GetRoomsByCategory(CategoryId, SelectedDate, NumberNights, NumberPeoples));

您需要传递RoomModel 的实例而不是System.Data.Entity.Infrastructure.DbQuery&lt;Hotel.BusinessObject.Room&gt;。我建议将您的控制器代码更改为以下

public ActionResult ComfortLevelView(int NumberNights, int CategoryId, int NumberPeoples, DateTime SelectedDate)
{
    IRoomService roomService = new RoomService();
    var rooms = roomService.GetRoomsByCategory(CategoryId, SelectedDate, NumberNights, NumberPeoples);

    RoomModel model = new RoomModel();
    model.Rooms = rooms.ToList();

    return View(model);
}

【讨论】:

  • 是的!!!对我很有帮助!!但是 public ActionResult ComfortLevelView(RoomModel model) //model 还是 null(
  • 您没有说模型在问题中为空,并且您已经接受了我的回答。如果您还有其他问题,则需要ask a new question
  • @Alex,回到你的上一个问题,阅读我的 cmets。没有什么可以回发到您的方法,因为没有什么可以回发。 @Html.DisplayFor() 不生成表单控件。
  • 那么如何显示文字呢?我试过标签、文本框、显示
  • @Alex,请不要一直在 cmets 中提出越来越多的问题。您已经接受了我的回答,并且您在 cmets 中提出的问题已经超出了这个问题的范围。 ask a new question 或回到 your previous question,正如 Stephen Muecke 所建议的那样。如有需要,您可以edit your previous question
猜你喜欢
  • 2013-03-16
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 2023-04-09
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多