2012-07-06 10 views
2

我正在研究Web應用程序的設計。該應用程序最初並不是由我編碼的,並且我在嘗試設計時儘量不要修改任何控制器或模型。將信息從模型拉入局部視圖

此刻,應用程序是一個標題,應該顯示您正在查看的項目的名稱和項目的完成日期。此標題是佈局調用的局部視圖,也是局部視圖。因爲標題是一個單獨的視圖,所以在項目詳細信息視圖中,我很難抽出這些信息。

我試着給控制器添加一個@model引用給Header,但得到了一個錯誤,我傳入的錯誤類型(請參閱下面的錯誤)。

我也嘗試將模型傳遞給調用標頭的Layout視圖中的@ Html.Partial調用(也是this)。一般來說一切我曾嘗試讓我這個錯誤:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[OST.Pride.Business.Models.Project]', but this dictionary requires a model item of type 'OST.Pride.Web.Models.AdminProjectUserEdit'. 

我不能隨便動頭項目詳細信息視圖,因爲頭是整個應用程序的佈局的一部分。我會很感激任何人在這方面的幫助,我已經停留了一段時間。

頭被稱爲佈局

<header id="main-header"> 
    @Html.Partial("LayoutTemplates/Header") 
</header> 

標題:

<div class="navbar" style="padding-top:15px;"> 
    <div class="navbar-inner"> 
     <div class="container" style="width:auto;">   
      <div class="project-navbar"> 
       <h3><ul class="nav pull-left">Customer: TEST </ul></h3> 
       <h3><ul class="nav" style="padding-left:40%;">Project: //This is where the project name needs to be//</ul></h3> 
       <h3><ul class="nav pull-right" align="center">Project Completion Date: //Again, I need to pull the completion date here// </ul></h3> 

      </div> 
     </div> 
    </div> 
</div> 

控制器的方法

public ActionResult Details(int? projectid, int? ProjectID, int? projectuserid) // based on the project id, populates the view with the roles, project users, and the roles for the screen. 
     { 
      var model = new Models.AdminProjectUserEdit(); 

      model.Project = storeDB.Projects.Find(ProjectID); 
      model.Users = storeDB.Users.ToList(); 

      model.Surveys = storeDB.Surveys.Include("SurveyType").ToList(); 
      model.ProjectUsers = storeDB.ProjectUsers.Include("Project").Include("User").Where(x => x.ProjectID == projectid).ToList(); 
      return View(model); 
     } 

回答

1

儘管Darin的答案非常好,在不同的情況下可能會更好,但我決定採用我的主管a的建議nd使用ViewBag。使用這種方法意味着我不需要將模型傳遞到頭部,這是一個共享視圖。

這裏是我是如何實現這個方法到我的代碼:

頭的佈局被稱爲

<header id="main-header"> 
    @Html.Partial("LayoutTemplates/Header") 
</header> 

<div class="navbar" style="padding-top:15px;"> 
    <div class="navbar-inner"> 
     <div class="container" style="width:auto;" > 

     @if (@ViewBag.projectName == null) 
     { /**do nothing**/ } 
     else 
     { 

      <div class="project-navbar"> 
       <h3><ul class="nav pull-left">Customer: TEST </ul></h3> 
       <h3><ul class="nav" style="padding-left:40%;">Project: @ViewBag.projectName</ul></h3> 
       <h3><ul class="nav pull-right" align="center">Project Completion Date: @ViewBag.projectCompletion </ul></h3> 

      </div> 
     } 
     </div> 
    </div> 

控制器的方法

public ActionResult Details(int? projectid, int? ProjectID, int? projectuserid) // based on the project id, populates the view with the roles, project users, and the roles for the screen. 
    { 
     var model = new Models.AdminProjectUserEdit(); 

     model.Project = storeDB.Projects.Find(ProjectID); 
     model.Users = storeDB.Users.ToList(); 

     model.Surveys = storeDB.Surveys.Include("SurveyType").ToList(); 
     model.ProjectUsers = storeDB.ProjectUsers.Include("Project").Include("User").Where(x => x.ProjectID == projectid).ToList(); 
     ViewBag.projectName = storeDB.Projects.Find(ProjectID).ProjectName; 
     ViewBag.projectCompletion = storeDB.Projects.Find(ProjectID).ProjectCompletion; 
     return View(model); 
    } 
3

這裏有幾點考慮。既然你把它放在佈局中,這意味着你的應用程序中絕對每個視圖都會顯示項目細節。但要顯示項目細節,您必須首先有一個項目。並且有一個項目,你必須有一個項目ID。遵循這個邏輯,這反過來意味着您必須在每個請求的每個地方都有項目ID。該項目ID可能是請求查詢字符串或路由的一部分。爲了演示的目的,我假定它是路由數據的一部分。

所以,你可以使用一個child action顯示佈局內的項目,而不是細節的部分觀點:

<header id="main-header"> 
    @Html.Action("Header", "SomeController", new { projectid = ViewContext.RouteData["projectid"] }) 
</header> 

注意我們是如何傳遞projectId參數從路由數據。如果項目ID位於查詢字符串中,或​​者Cookie或任何您決定保留它的地方,您顯然必須修改此代碼。這條路線似乎是一個好地方。

所以我們調用Some控制器的Header兒童行動對行動,這將獲取該項目,並把它傳遞給相應的局部視圖:

[ChildAction] 
public ActionResult Header(int? projectId) 
{ 
    if (projectId == null) 
    { 
     return PartialView(); 
    } 
    var project = storeDB.Projects.Find(projectID); 
    return PartialView(project); 
} 

,然後我們渲染項目的細節相應Header.cshtml局部視圖中:

@model Project 
@if (Model == null) 
{ 
    // there wasn't any project selected => display some default stuff 
    <div>No project to display</div> 
} 
else 
{ 
    <div class="navbar" style="padding-top:15px;"> 
     <div class="navbar-inner"> 
      <div class="container" style="width:auto;">   
       <div class="project-navbar"> 
        <h3> 
         <ul class="nav pull-left"> 
          Customer: TEST 
         </ul> 
        </h3> 
        <h3> 
         <ul class="nav" style="padding-left:40%;"> 
          Project: 
          @Html.DisplayFor(x => x.Name) 
         </ul> 
        </h3> 
        <h3> 
         <ul class="nav pull-right" align="center"> 
          Project Completion Date: 
          @Html.DisplayFor(x => x.CompletionDate) 
         </ul> 
        </h3> 
       </div> 
      </div> 
     </div> 
    </div> 
} 
+0

好的,謝謝你的幫助。我非常確定這正是我需要的,我只需要弄清楚如何正確實施這個項目。我會及時通知你的。 – LemonFlip 2012-07-06 17:55:01

+0

projectID參數是一個路由,所以我現在要開始調整它。如果您想相應地更新您的答案,請隨時免費,否則我仍計劃在完成並運行後再對其進行標記。再次感謝。 – LemonFlip 2012-07-06 18:00:02

+0

答覆已更新。 – 2012-07-06 18:24:22