如何将Linq结果转换为json并在mvc3视图中使用该json?

时间:2014-01-28 07:04:06

标签: asp.net-mvc razor

我的模特是:

public class FloorNames
{
    [Key]
    public int FloorId { get; set; }

    [Required(ErrorMessage = "FloorName required")]
    [StringLength(160)]
    public string FloorName { get; set; }
}

public class RoomDetails
{

    [Key]
    public int RoomDetailsId { get; set; }

    [Required(ErrorMessage = "RoomNo required")]        
    public int RoomNo { get; set; }

    public int RoomTypeId { get; set; }
    [Required(ErrorMessage = "Adult required")]

    public int Adult { get; set; }
    [Required(ErrorMessage = "Child required")]

     public int Child { get; set; }
    [Required(ErrorMessage = "Child required")]

     public bool ExtraBed { get; set; }

    [Required(ErrorMessage = "ExtraBedNumbers required")]        
     public int ExtraBedNumbers { get; set; }

    [Required(ErrorMessage = "RoomFloorNo required")]        
    public int FloorId { get; set; }

    [Required(ErrorMessage = "RoomRate required")]        
     public int RoomRate { get; set; }

    public virtual RoomType RoomType { get; set; }
    public virtual FloorNames FloorNames { get; set; }
}


public class RoomType
{
    public int RoomTypeId { get; set; }
    public string RoomTypeName { get; set; }

}

控制器类是:

public class ReceptionController : Controller
{

    private MVCLodgeEntities db = new MVCLodgeEntities();

    public ActionResult Index()

    {
        var roomdetails = db.RoomDetails.Include(r => r.RoomType).Include(r => r.FloorNames);

        roomdetails = roomdetails.OrderByDescending(s => s.FloorNames.FloorName);


        return View(roomdetails.ToList());
    }


}

在我看来,我写了代码:

@model IEnumerable<MvcLodge.Models.RoomDetails>

@{

    ViewBag.Title = "Index";

 }

<h2>Reception</h2>

<p>
    @Html.ActionLink("Create New", "Create")

</p>

<table>

@{

    var fn="";

    var i = 1;

}  

@foreach (var item in Model)
{

    if (i == 1)
    {

       fn = "II Floor";
    }

    if (fn == item.FloorNames.FloorName)
    {

        fn = item.FloorNames.FloorName;

        <td>

             @Html.DisplayFor(modelItem => item.RoomNo)- @Html.DisplayFor(modelItem => item.RoomType.RoomTypeName)

             @Html.DisplayFor(modelItem => item.Adult) -  @Html.DisplayFor(modelItem => item.Child)-@Html.DisplayFor(modelItem => item.FloorNames.FloorName)

             <a href="@Url.Action("Details", "Store", new { id = item.RoomNo})"> 

                 <img alt="@item.RoomNo" src="../../Images/abs.png" />

             </a> 

        </td>

    }

    else
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RoomNo)- @Html.DisplayFor(modelItem => item.RoomType.RoomTypeName)<br/>

                @Html.DisplayFor(modelItem => item.Adult) -  @Html.DisplayFor(modelItem => item.Child)-@Html.DisplayFor(modelItem => item.FloorNames.FloorName)<br/>

               <a href="@Url.Action("Details", "Store", new { id = item.RoomNo})"> 

                   <img alt="@item.RoomNo" src="../../Images/abs.png" /> 

              </a> 

           </td>

      </tr>

    }

}

</table>

我的输出类似于:

101 Ac III Floor

102 AC II Floor    103 AC II Floor

104 AC IFloor

105 AC IFloor

但我希望在同一行显示所有I楼层,并且像

那样显示副版本
101 Ac III Floor

102 AC II Floor    103 AC II Floor

104 AC I Floor      105 AC I Floor

请帮帮我......

0 个答案:

没有答案