How to show a controller's ViewBag data on another page

jewel 1,006 Reputation points
2024-10-27T15:34:02.4166667+00:00

I want to see some data from one of my controllers(Finddata) in another controller's view(Index). I want to use viewbag method for this. But unable to send the data. I would appreciate it if someone could help.

 public IActionResult Index()
  {
     
return View();
  }
		
  public IActionResult Finddata(DateTime p1)
  {
      ViewBag.result = _context.tbl_Sells.Where(x=>x.Date== p1).Sum(x => x.Value);
      return View("Index");
  }
//Index.html view
<label>@ViewBag.result</label>
    <a href="#" onclick="loadRecordByDate()">Click Here</a>
    <input type="date" id="clander"  />
@section Scripts {
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
 <script type="text/javascript">
        function loadRecordByDate() {
            var date = $("#clander").val()
        $.ajax({
                url: '/Dashbord/Finddata?p1=' + date
           
        });
        };    
    </script>
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,605 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 4,720 Reputation points Microsoft Vendor
    2024-10-28T02:27:34.0066667+00:00

    Hi @jewel,

    If you want to display the ViewBag by ajax call back from the Finddata action, you'll need to make sure that the Finddata method returns the result data without fully re-rendering the Index view.

    Index.cshtml

    <label id="resultLabel">@ViewBag.result</label>
    <a href="#" onclick="loadRecordByDate()">Click Here</a>
    <input type="date" id="clander" />
    @section Scripts
    {
        <script>
            function loadRecordByDate() {
                var date = $("#clander").val();
                $.ajax({
                    url: '/Dashbord/Finddata?p1=' + date,
                    type: 'GET',
                    success: function(response) {
                        console.log(response)
                        $("#resultLabel").text(response.result); // Update the label with result
                    },
                    error: function() {
                        alert("An error occurred while fetching data.");
                    }
                });
            }
        </script>
    }
    

    DashbordController

    public IActionResult Finddata(DateTime p1)
    {
        ViewBag.result = _context.tbl_Sells.Where(x => x.Date == p1).Sum(x => x.Value);
        return Json(new { result=ViewBag.result  as int?});
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,461 Reputation points
    2024-10-27T17:04:55.05+00:00

    Your goal is not clear. your action Finddata returns the index actions html, but your JavaScript Ajax call does nothing with the response. Maybe you wanted to load the html, so instead of Ajax, you wanted a form post.

    0 comments No comments

  2. SurferOnWww 3,041 Reputation points
    2024-10-28T01:19:48.6366667+00:00

    I want to see some data from one of my controllers(Finddata) in another controller's view(Index). I want to use viewbag method for this.

    Using ViewBag is not possible or at least not practical to transfer the data in such way. I recommend that you try to find the other measures such as:

    (1) Use action method with partial view.

    or

    (2) Use action method which return the required data in JSON format.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.