部分视图错误

时间:2011-02-22 19:06:33

标签: asp.net-mvc partial-views

我已将代码从我的视图转移到部分视图并收到错误

我的观看代码是:

          function renderSeason(){             $('#visSeasonbut')。click(function(){$('#p2')。load(this.href); return false;});         }         function renderGame(){                       $('#visGamebut')。click(function(){$('#p2')。load(this.href); return false;});         }                                                                           
                         <div id="game">                            
                           <%= Html.ActionLink("Game 1", "PitcherTitles", "Test", null, new { id = "visGamebut" })%>   </div>

LiveGame.Models.Baseball.Player visPlayer1 = ViewData.Model.GetCurrentTeamPlayer(false);                                              if(visPlayer1!= null)                                              {                                                  LiveGame.Models.Baseball.Player rosterPlayer = ViewData.Model.GetTeam(0).RosterDictProp.GetPlayer(visPlayer1.Player_IdProp);

                                             if (ViewData.Model.InningHalfProp == true)
                                             { %>

                                            show Pitcher Stats here
                                      <%


                                             }
                                             else
                                             { 
                                      %>
                                     Show PitchTitles here                                            


                                    <% 
                                             }
                                             }

和部分观点是这样的:

&lt;%@ Control Language =“C#”Inherits =“System.Web.Mvc.ViewUserControl”%&gt;

                                                                                                 胜                                                 损失                                                 时代                                                                                                                                                                                                                                                                                                                                              

第二个是:

&lt;%@ Control Language =“C#”Inherits =“System.Web.Mvc.ViewUserControl”%&gt;

                          IP                               H                               [R                               ER                               BB                               所以                               HR                               时代                                                                                                                                                                                                                                                                                                  

当我点击链接时,我收到错误,“rosterPlayer”未找到,请提出解决方案。感谢

1 个答案:

答案 0 :(得分:1)

渲染此部分时,请确保传递模型:

<% Html.RenderPartial("SomePartialName", Model); %>

当然不要忘记强烈地输入您的部分模型:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.SomeModelType>" 
%>

同样根据您发布的评论,您似乎没有将任何模型传递给您的观点。因此,请更改此设置并确保传递模型,否则无法使用Model属性:

public ActionResult PitcherStats() 
{ 
    Game currentGame = new Game(); 
    return View(currentGame); 
} 

public ActionResult PitchTitles() 
{ 
    Game currentGame = new Game(); 
    return View(currentGame); 
}