2016-09-20 1 views
0

私はホームページに置くだけのグリッドを持っていますが、Viewで複数のグリッドを返すと、エラーが表示されます。MVCホームページで複数のグリッドのVIEWを返す方法は?

私は新しい例外をスローしようとすることができますが、それでもエラーが発生すると言いました。複数のグリッドを表示するにはどうすればいいですか?グリッドはモデルのリストに作成されます。

@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

これを達成するが、それ自体が、見るだけで、その中に単一のモデル供給を持つことができるいくつかの方法があります。

両方のモデルを含むViewModelクラスを作成することもできます。また、1つのViewを複数のPartialViewを持つモデルにリファクタリングすることもできます。

0

これは

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

がどのようにビューは、単一のモデルを持つことができます

@Html.Grid(Model)? 

ビューにマッピングするかを知っている、いずれかの単一のモデルや休憩にこれらEnumerablesを組み合わせてしまう無効です自分の意見を持つことができる部分にビューをダウン

グリッドのないプライマリビューと、それぞれのグリッドの正しいモデルを取得して接続するために、接続されたコントローラのアクションでグリッドをラップする2つのパーシャルがあります。ビューの使用Toupleで複数のモデルリストを使用するか、メインモデルを追加し、その中にプロパティとしてモデルのリストを割り当てるには

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

0

あなたがそれをするビューも仕事でモデルリストの2種類を受け入れていますモデル

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

関連する問題