2016-09-20 80 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