Hi
I have tow query type models that I want to get their data by ajax in one view with Entity Framework Core.
I'm trying to pass the number of records returned by Ajax to the caller view by session, But no value returned.
HolidaysHeader model:
public class HolidaysHeader { public int Id { get; set; } public string Name{ get; set; } }
ActionsHeader model:
public class ActionsHeader { public int Id { get; set; } public string Name{ get; set; } }
ApplicationDBContext:
public virtual DbQuery<ActionsHeader> ActionsHeader { get; set; } public virtual DbQuery<HolidaysHeader > HolidaysHeader { get; set; }
Actions get data method:
public async Task<List<ActionsHeader>> ActionsHeaderE(decimal? sid) { var nContext = _context.Query<ActionsHeader>().AsQueryable(); if (sid != null || sid.GetValueOrDefault() != 0) { nContext = nContext .Where(s => s.Id.ToString().Contains(sid.ToString())); } nContext = nContext.OrderByDescending(s => s.Id).Take(1000); var data = await nContext.ToListAsync();HttpContext.Session.SetString("A", data.Count().ToString()); return data; }
method of performing Ajax:
public IActionResult All() {ViewBag.A = HttpContext.Session.GetString("A"); return View(); }
All view which I tried to return number of records returned by Ajax (@ViewBag.A):
<form>
<div class="col-xs-2">
<label for="ex2">Id</label>
<input class="form-control" type="text" id="sidd" name="sid">
</div>
<input type="button" value="بحث" id="search" class="btn btn-default">
</form>
<div>
<ul class="nav nav-tabs" style="color:black;">
<li class="in active" style="color:black;"><a data-toggle="tab" href="#home">@ViewBag.A Actions</a></li>
<li><a data-toggle="tab" href="#menu1" style="color:black;">Holidays</a></li>
</ul>
@*Some code*@
<div class="tab-content" id="col-md-12">
<div id="home" class="tab-pane fade in active">
<table class="table">
<thead>
<tr>
<th>
Id
</th>
<th>
Name
</th>
</tr>
</thead>
<tbody id="ActionsEtags"></tbody>
</table>
</div>
</div>
</div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}<script>$(function () {$('#search').on('click', function () { var sid = $('#sidd').val();$.ajax({ type: "GET", url: "/MyController/ActionsHeaderE?sid=" + sid, success: function (data) {$("#ActionsEtags").empty();$.each(data, function (index, item) { console.log(item);$('<tr id="Row' + index + '">' + '<td>' + item.Id + '</td>' + '<td>' + item.Name + '</td>' + '</tr>').appendTo($('#ActionsEtags')); }); } }); }); });</script> }
But when I pressed Search button the records are returned successfully but with blank space for @ViewBag.A. Why? and How to solve please?