我有一个ajax链接,它传递了一个参数,但是打开的页面不需要该参数。该页面只加载2个局部视图,其中一个需要该参数传递到页面以正确加载数据,另一个仅需加载表单,因此不需要该参数。我该如何实现这一点?
我有一个ajax链接,它传递了一个参数,但是打开的页面不需要该参数。该页面只加载2个局部视图,其中一个需要该参数传递到页面以正确加载数据,另一个仅需加载表单,因此不需要该参数。我该如何实现这一点?
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
ViewData["NeededID"] = id
return View(sysfunctions);
然后在您呈现局部视图的位置
<%= Html.RenderPartial("GridFunction", (int)ViewData["NeededID"]) %>
当然需要根据需要进行转换。
无论作为第二个参数推送的是什么,都会成为部分视图中的.Model。我建议对部分视图进行强类型化。
试试这个:
<% Html.RenderPartial("GridFunction", new ViewDataDictionary {{"Id", ViewData["Id"]}}); %>
更新:
在您的控制器操作中添加以下内容:
ViewData["Id"] = Id;
更新:
在您的“GridFunction”局部视图中,可以通过以下方式访问Id:
<%= ViewData["Id"] %>
//控制器
public ActionResult EditFunctions(int id)
{
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
return View(sysfunctions);
}
// This is the controller (it does no need the parameter "ID")
//这是视图“EditFunctions”
<div id="formFunction">
<% Html.RenderPartial("FormFunction"); %>
</div>
<div id="gridFunction">
<% Html.RenderPartial("GridFunction"); %> // The grid needs the ID to work correctly but its in the parent page not in the partial call....and the call is an ajax call
</div>