简体   繁体   English

如何将值从控制器动作方法传递到局部视图

[英]How to pass value from controller action method to partial view

Here I tried using TempData to pass data from controller action method to partial view. 在这里,我尝试使用TempData将数据从控制器操作方法传递到局部视图。 But it does not work. 但这行不通。

My Controller Code 我的控制器代码

public class PropertyController : Controller
{
    public ActionResult Index()
    {
        return View();
    }


    public ActionResult GetRequestReport()
    {
        List<ReportsInfo> reportsList = reportsManager.GetRequestReport(UserName, Locality, "usp_GetResults");
    int count = reportsList.Select(x => x.UserId).Distinct().Count();
    TempData["Count"] = count;
        return PartialView("_Partial1", reportsList);
    }

    public ActionResult Test(string id)
    {
        switch (id)
        {
            case "tab1":
                return PartialView("_Results");
            case "tab2":
                return PartialView("_Results2");
        }

        return new EmptyResult();
    }

}

Index.cshtml Index.cshtml

<div id="div-for-partial">
                </div>


    $(function () {
        $('.tab').click(function () {
            var id = this.id;
            $.get('@Url.Action("Test")', { "id": id }, function (data) {
                $('#div-for-partial').html(data);
                $('.mvc-grid').mvcgrid();
            });
        });
    });

_Results Partial View _结果局部视图

   @TempData["Count"]
<br />
 <div id="reportList">
                    @Html.AjaxGrid(Url.Action("GetRequestReport", "Property"))
                </div>

_Partial1 Partial View _Partial1局部视图

@model Project.Models.ReportsInfo
@(
        Html.Grid(Model)
            .Build(columns =>
            {

                columns.Add(model => model.ApproverName).Titled("Approver Name");

            })
            .Empty("No records found.")
            .Sortable()
            .Pageable(pager =>
            {
                pager.RowsPerPage = 15;
            })
)

I have even tried view bag but no luck is there any way to achieve it. 我什至尝试过查看包,但没有运气可以实现它。 Basically I want to pass count of results in TempData and use in partial view 基本上我想在TempData中传递结果计数并在局部视图中使用

public ActionResult GetRequestReport()
    {
        List<ReportsInfo> reportsList = reportsManager.GetRequestReport(UserName, Locality, "usp_GetResults");
    int count = reportsList.Select(x => x.UserId).Distinct().Count();
    ViewData["Count"] = count;
        return PartialView("_Partial1");
    }

In Partial1 partial View, you can get count in a count variable as shown below and use it where ever you want to. 在Partial1部分视图中,您可以在count变量中获得count,如下所示,并可以在任何需要的地方使用它。

@(
        int count=(int)ViewData["Count"];

        Html.Grid(Model)
            .Build(columns =>
            {

                columns.Add(model => model.ApproverName).Titled("Approver Name");

            })
            .Empty("No records found.")
            .Sortable()
            .Pageable(pager =>
            {
                pager.RowsPerPage = 15;
            })
)

you never called GetRequestReport() before calling Results partial view, how do you expect to get the value of count passed to Results partial view. 您从未在调用Results局部视图之前调用过GetRequestReport(),您如何期望获得传递给Results局部视图的count值。 Please verify what you are asking. 请验证您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM