【问题标题】:partial view error部分视图错误
【发布时间】:2011-02-23 04:11:55
【问题描述】:

我已将以下代码从我的视图移至部分视图并出现错误

我的查看代码是:

函数渲染季节(){ $('#visSeasonbut').click(function() { $('#p2').load(this.href); return false; }); } 函数渲染游戏(){ $('#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                                            


                                    <% 
                                             }
                                             }

部分视图是这样的:

获胜 损失 时代

第二个是:

知识产权 H R 急诊室 BB 所以 人力资源 时代

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

【问题讨论】:

  • 你能把你的代码发布在 action 方法中吗?
  • public ActionResult PitcherStats() { Game currentGame = new Game();返回视图(); } 公共 ActionResult PitchTitles() { 游戏 currentGame = new Game();返回视图(); }
  • 不要在评论中粘贴代码 - 它难以辨认。相反,更新您的问题。此外,由于您没有在 return View() 语句中返回模型,因此您的视图没有可使用的模型 - 它是空的,就像错误所说的那样。
  • 提示和技巧:使用&lt;%: %&gt; 而不是&lt;%= Html.Encode(...) %&gt;(假设您使用的是asp.net mvc 版本2)。
  • 现在我正在使用 MVC 版本 1

标签: asp.net-mvc partial-views


【解决方案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); 
} 

【讨论】:

  • 我使用 jquery 渲染如下:第 1 季 Game 1 和 javascript 代码是:
  • @asif,好的,然后在 PitcherStatsPitchTitles 操作中返回视图时传递一个模型。
  • 在我的情况下如何通过它?在 hrefs 或 javascript 函数或操作方法中?
  • 另外,我可以不用 jquery 做同样的事情吗?
  • 什么一样的东西?您没有解释您的方案是什么以及您要做什么。您在问题中所说的只是您将一些代码从您的视图移到了部分。显然这些信息是不够的。
猜你喜欢
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多