简体   繁体   中英

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. 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

<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

@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

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.

@(
        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. Please verify what you are asking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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