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