2016-09-20 56 views
0

我有多個網格我想提出只在主頁上,但是當我在查看返回多於一個,就說明我的錯誤。MVC如何爲主頁上有多個網格返回查看?

它說我可以嘗試拋出新的異常,但仍然是一個錯誤。我如何讓它顯示多個網格?網格在模型的列表中創建。

@using GridMvc.Html; 
@model IEnumerable<Website.Models.Activities> 
@model IEnumerable<Website.Models.Cities> 

@{ 
     ViewBag.Title = "Index"; 
} 
<head> 
    <link href="@Url.Content("~/Content/Homestyle.css")" rel="stylesheet" type="text/css" /> 
</head> 

<body> 
    <h1 align="center" id="homewords">Home</h1><br/><br/><br/> 
    <p id="mainp" align="center"> 
     Welcome to What2Do, we are a website to help you find things to do around town as a local or a tourist. We are a brand new website 
     and we are still looking for more places and cities in the future. If you have any good ideas or know good places, please see the about 
     page to email this information! Hope you enjoy! :) 
    </p> 
    <br /><br /> 
    <div id="city"> 
     <p id="choosecity">City: </p> 
     @Html.Grid(Model).Columns(cols => 
    { 
     cols.Add(c => c.stpete).Titled("St. Petersburg"); 
     cols.Add(c => c.tampa).Titled("Tampa"); 
     cols.Add(c => c.orlando).Titled("Orlando"); 
    }); 

</div>  
<div id="act" align="center"> 
     <p id="activity">Activity: </p> 
     <select id="ddlactivity" name="activity" size="4"> 
      <option value="1">Theme Parks</option> 
      <option value="2">Outdoor Activities</option> 
      <option value="3">Beach Activities</option> 
      <option value="4">Bars and Music</option> 
     </select> 
    </div> 
    <br/><br/><br/><br/> 

    <div id="results" width="500px"> 

     @Html.Grid(Model).Columns(columns=> 
    { 
     columns.Add(c => c.activity).Titled("Activity"); 
     columns.Add(c => c.location).Titled("Location"); 
     columns.Add(c => c.cost).Titled("Cost"); 
     columns.Add(c => c.hours).Titled("Hours"); 

    }).WithPaging(3).Sortable(true) 

    </div> 

控制器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Website.Models; 

namespace Website.Controllers 
{ 
    public class HomeController : Controller 
    { 
     // GET: Default 
     public ActionResult Index() 
     { 
      var citymodel = new Cities(); 
      List<Cities> citygrid = citymodel.city(); 
      var model = new Activities(); 
      List<Activities> actgrid = model.Acts(); 

      return View(actgrid, citygrid); 

     } 
+4

使用爲每一集的性能視圖模型 –

回答

0

有許多的方法可以做到這一點,但在其本身,視圖只能有一個單一的模式喂進去。

您可以創建一個包含這兩種模型視圖模型類或者你可以重構你的一個視圖到一個與它的幾個PartialViews,每一個都具有模型送入它。

0

這是無效

@model IEnumerable<Website.Models.Activities> 
@model IEnumerable<Website.Models.Cities> 

的看法如何知道映射哪一個

@Html.Grid(Model)? 

視圖只能有一個單一的模式,這兩種可枚舉合併成一個單一的模式或破將觀點分解成可以有自己觀點的部分。

你將不得不您與它沒有電網,然後有兩隻諧音每個纏繞與每個部分獲取和安裝該特定網格正確的模型連接控制器操作的網格主視圖。

0

您接受兩點式模型列表在你看來它會也不工作:

@model IEnumerable<Website.Models.Activities> 
@model IEnumerable<Website.Models.Cities> 

要考慮使用Touple使用多個型號列表,或添加主模型,然後在分配模型的列表作爲財產模型

Touple:

model Tuple<IEnumerable<Website.Models.Activities>,IEnumerable<Website.Models.Cities>>; 

實施例:

類別:

public class User 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string ContactNo { get; set; } 
} 

public class UserEducation 
{ 
    public int Id { get; set; } 
    public int UserId { get; set; } 
    public int PassingYear { get; set; } 
    public string Education { get; set; } 
} 

控制器:

[HttpGet] 
public ActionResult Index() 
{ 
    List<User> userList = new List<User>(); 
    for (int i = 1; i < 4; i++) 
    { 
     User objUser = new User(); 
     objUser.Id = i; 
     objUser.Name = "Name" + i.ToString(); 
     userList.Add(objUser); 
    } 

    List<UserEducation> userEducationList = new List<UserEducation>(); 
    for (int i = 1; i < 4; i++) 
    { 
     UserEducation objUserEducation = new UserEducation(); 
     objUserEducation.Id = i; 
     objUserEducation.Education = "Education" + i.ToString(); 
     objUserEducation.PassingYear = 2015+i; 
     userEducationList.Add(objUserEducation); 
    } 

    return View(new Tuple<List<User>, List<UserEducation>>(userList, userEducationList)); 
} 

檢視:

@model Tuple<List<TestMVCApp.Models.User>, List<TestMVCApp.Models.UserEducation>> 
@{ 
    Layout = null; 
} 

<style> 
    tr th,tr td{ 
     padding:10px; 
    } 
    </style> 
    <body style="margin-left:20px"> 
    <div style="float:left;clear:both;font-weight:bold;color:red;font-size:20px;"> 
    Users: 
</div> 
<div style="float:left;clear:both"> 

    <table> 
     <tr> 
      <th> 
       Id 
      </th> 
      <th> 
       Name 
      </th> 
     </tr> 
     @foreach (var item in Model.Item1) 
     { 
      <tr> 
       <td>@item.Id</td> 
       <td>@item.Name</td> 
      </tr> 
     } 
    </table> 


</div> 
<div style="float:left;clear:both;font-weight:bold;color:red;font-size:20px;"> 
    User Education: 
</div> 
<div style="float:left;clear:both"> 

    <table> 
     <tr> 
      <th> 
       Id 
      </th> 
      <th> 
       Education 
      </th> 
      <th> 
       PassingYear 
      </th> 
     </tr> 
     @foreach (var item in Model.Item2) 
     { 
      <tr> 
       <td>@item.Id</td> 
       <td>@item.Education</td> 
       <td>@item.PassingYear</td> 
      </tr> 

     } 
    </table> 
</div> 

Outpu T:

enter image description here

相關問題