我有一个 MVC 应用程序,当单击链接时,需要根据另一个页面上的相同值显示页面。我不知道为什么传递的是 null 而不是字符串。我的代码如下。
控制器:
public string searchQ
{
get { return (string)Session["searchQ"]; }
set { Session["searchQ"] = value; }
}
public ActionResult Index()
{
Session["InitialLoad"] = "Yes";
return View();
}
[HttpPost]
public ActionResult Index(string heatSearch)
{
ViewBag.SearchKey = heatSearch;
searchQ = heatSearch;
return View();
}
public ActionResult Index_Perm()
{
ViewBag.SearchKey = searchQ;
return View();
}
public ActionResult PartialMainLim(string heatSearch)
{
HomeModel C = new HomeModel();
ChemViewModel D = new ChemViewModel();
D = C.QueryResults(heatSearch);
return PartialView(D);
}
public ActionResult PartialMain(string heatSearch)
{
HomeModel C = new HomeModel();
ChemViewModel D = new ChemViewModel();
D = C.QueryResults(heatSearch);
return PartialView(D);
}
索引视图中的代码如下所示(这个有效):
@if (ViewBag.SearchKey != null)
{
<div>
@Html.Action("PartialMainLim", "Home", (string)ViewBag.SearchKey)
</div>
}
在 index_perm 视图中:
@if(ViewBag.SearchKey != null)
{
<div>
@Html.Action("PartialMain", "Home", (string)ViewBag.SearchKey)
</div>
}
当我在两个视图中检查 SearchKey 的值时,它是正确的。然而,尽管 SearchKey 是正确的,但对于“PartialMain”方法,传递的是 null 而不是字符串。不过,这一切都适用于另一种观点。我究竟做错了什么?