ASP.NET MVC如何将页面参数传递到部分视图?

6

我有一个ajax链接,它传递了一个参数,但是打开的页面不需要该参数。该页面只加载2个局部视图,其中一个需要该参数传递到页面以正确加载数据,另一个仅需加载表单,因此不需要该参数。我该如何实现这一点?

4个回答

6
为了实现您想要的功能,您需要将id添加到ViewData构造中。
var sysfunctions= UnisegurancaService.FunctionsRepository.All();
ViewData["NeededID"] = id
return View(sysfunctions);

然后在您呈现局部视图的位置

<%= Html.RenderPartial("GridFunction", (int)ViewData["NeededID"]) %>

当然需要根据需要进行转换。

无论作为第二个参数推送的是什么,都会成为部分视图中的.Model。我建议对部分视图进行强类型化。


没问题。我已经让我的回答更有帮助了。如果你不想强制类型化你的部分视图(虽然我建议你要对它们进行类型化),那么 eu-ge-ne 提供的 ViewDataDictionary 的建议更好。 - Chad Ruppert

3

试试这个:

<% Html.RenderPartial("GridFunction", new ViewDataDictionary {{"Id", ViewData["Id"]}}); %>

更新:

在您的控制器操作中添加以下内容:

ViewData["Id"] = Id;

更新:

在您的“GridFunction”局部视图中,可以通过以下方式访问Id:

<%= ViewData["Id"] %>

0

//控制器

  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>

你的GridFunction部分视图是否强类型化? - eu-ge-ne

0
如果页面的某些依赖需要该参数,则页面需要知道足够的信息来传递数据,因此该页面应该能够提供数据。或者更简单地说,只需将参数添加到页面的视图数据中即可完成。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接