2

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

sgl
  • 563
  • 1
  • 6
  • 16
  • return PartialView("_Results", DatesYouWantToPass); and then you can use them in the partialView with @Model check it https://stackoverflow.com/questions/20799658/how-can-i-pass-parameters-to-a-partial-view-in-mvc-4/20808352 – Jordi Jordi Aug 09 '18 at 16:53
  • Can you please show sample code of this scenrio – sgl Aug 09 '18 at 16:58
  • switch (id) { case "tab1": string answer = "Hello"; return PartialView("_Results", answer); case "tab2": return PartialView("_Results2"); } – Jordi Jordi Aug 09 '18 at 17:02
  • Tab is not getting called when search button inside tab is clicked. So action will not invoke – sgl Aug 09 '18 at 17:05
  • 1
    You are setting TempData item in `GetRequestReport` action method which returns the `_Partial1` partial view result. But you are trying to use TempData in `_Results`. Why do you expect that to work ? You can read the temp data value inside `_Partial1` – Shyju Aug 09 '18 at 17:14

2 Answers2

0
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;
            })
)
nazir_cinu
  • 66
  • 1
  • 6
  • But this is assigning value to count but how can i show on view and reportlist is not passed to partial so how will the grid bind – sgl Aug 09 '18 at 17:21
0

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.

nazir_cinu
  • 66
  • 1
  • 6
  • is there a way to pass data from getrequestreport to _Partial1 – sgl Aug 09 '18 at 17:51
  • @sql : Yes, check my previous answer, its passing the data from getrequestreport to _Partial1 but only when you invoke the **getrequestreport** action method first. – nazir_cinu Aug 10 '18 at 00:56