Я экспериментировал с Razor Pages. Для меня это довольно новая технология, но я считаю ее похожей на мой прошлый опыт работы со старой технологией asp. Я могу отображать Отфильтрованные данные отображаются, когда я использую JQuery и JSON.
//PageModel
public async Task<PageResult> OnGetAsync()
{
//Get users Role.
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId
if (userId == null)
{
Message = "Some Problem!! (UE)";
return Page();
}
IList<string> roles = await GetRoles(userId);
if (roles == null)
{
Message = "Some Problem!! (RE)";
return Page();
}
List<Process> processesForThisRole = _context.Processes.Where(a => roles.Contains(a.RoleId)).ToList();
ProcessList = new SelectList(processesForThisRole, nameof(Process.ProcessID), nameof(Process.Description));
return Page();
}
public async Task<IList<string>> GetRoles(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
var role = await _userManager.GetRolesAsync(user);
return role;
}
public JsonResult OnGetDisplayApplications(int currentProcess)
{
Message = "";
if (currentProcess == 0)
{
secondAppealDetail = new List<InputModel>();
var convertedblnk = JsonConvert.SerializeObject(secondAppealDetail);
return new JsonResult(convertedblnk);
}
secondAppealDetail = new List<InputModel>();
var result = _context.SecondAppeal
.Where(me => me.CurrentProcess == currentProcess)
.ToList()
.Select(me =>
new InputModel()
{
SecondAppealId = me.SeconAppealId,
NameofApplicant = me.NameofApplicant,
ApplicationNumber = me.ApplicationNumber,
PIODetails = string.Join(" ; ", me.CPIOName, me.CPIOAddress),
RTIOutcome = string.Join(" ", " RTI Date ", me.RTIDate.ToShortDateString(), ", RTI Response: ", me.CPIOResponse.ToString()),
DetailAddress = string.Join(" ; ", me.Address, me.TownVillage, me.StateName, me.DistrictName, me.PinCode),
DepartmentDetails = string.Join(" , ", me.DepartmentName, me.PublicAuthority),
}
);
secondAppealDetail = result.ToList();
var converted = JsonConvert.SerializeObject(secondAppealDetail);
return new JsonResult(converted);
}
// cs html файл
<h1>Process Applications</h1>
<div class="col-sm-4">
<label class="control-label">Select a Process</label>
<select asp-for="SelectedProcess" asp-items="Model.ProcessList" style="width:250px;max-width:250px;">
<option value="">Select Process</option>
</select>
</div>
<div class="alert alert-danger" role="alert">
SECOND APPEAL APPLICATIONS
</div>
<div>
<table class="table table-bordered table-responsive table-hover" id="tbProcess">
</table>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
$(function () {
$("#SelectedProcess").on("change", function () {
var processCode = $(this).val();
$.ajax({
type: "GET",
url: "/ApplicationProcess?handler=DisplayApplications",
contentType: "application/json",
dataType: "json",
data: { currentProcess: processCode },
success: function (response) {
//Remove Existing Content of the Table
$("#tbProcess tr").remove();
var trHTML = '<tr> <th>Application Number</th> <th>Name of Applicant </th><th>Detail Address</th><th>Department Details</th><th>PIO Details</th><th>RTI Outcome</th><th></th></tr>';
$.each(JSON.parse(response), function (index, item) {
trHTML += '<tr><td>'
+ item.ApplicationNumber + '</td><td>'
+ item.NameofApplicant + '</td><td>'
+ item.DetailAddress + '</td><td>'
+ item.DepartmentDetails + '</td><td>'
+ item.PIODetails + '</td><td>'
+ item.RTIOutcome + '</td><td> <a href="/ApplicationProcess/Details?id='
+ item.SecondAppealId + '&ProcessId='+processCode+'" target="_blank">Details</a></td></tr>';
});
$('#tbProcess').append(trHTML);
},
failure: function (response) {
alert(response);
}
});
});
});
</script>
}
Это также работает, когда я использую Раскройте и используйте кнопку для отправки формы
public async Task OnGetAsync(Guid? id,int SelectedValue)
{
//Get users Role.
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId
if (userId == null)
{
Message = "Some Problem!! (UE)";
// return Page();
}
IList<string> roles = await GetRoles(userId);
if (roles == null)
{
Message = "Some Problem!! (RE)";
// return Page();
}
List<Process> processesForThisRole = _context.Processes.Where(a => roles.Contains(a.RoleId)).ToList();
ProcessList = new SelectList(processesForThisRole, nameof(Process.ProcessID), nameof(Process.Description));
var currentProcess = 0;
if (SelectedValue == 0)
{
foreach (Process p in processesForThisRole)
{
currentProcess = p.ProcessID;
break; //Need the first one as default.
}
}
else
currentProcess = SelectedValue;
//Subsequent calls
var result = _context.SecondAppeal
.Where(me => me.CurrentProcess ==currentProcess)
.ToList()
.Select(me =>
new InputModel()
{
SecondAppealId = me.SeconAppealId,
NameofApplicant = me.NameofApplicant,
ApplicationNumber = me.ApplicationNumber,
PIODetails = string.Join(" ; ", me.CPIOName, me.CPIOAddress),
RTIOutcome = string.Join(" ", " RTI Date ", me.RTIDate.ToShortDateString(), ", RTI Response: ", me.CPIOResponse.ToString()),
DetailAddress = string.Join(" ; ", me.Address, me.TownVillage, me.StateName, me.DistrictName, me.PinCode),
DepartmentDetails = string.Join(" , ", me.DepartmentName, me.PublicAuthority),
}
);
secondAppealDetail = result.ToList();
}
PageModel
// cs html файл
<form asp-page="./Index" method="get">
<div class="form-actions no-color">
<p>
<div class="col-sm-4">
<label class="control-label">Select a Process</label>
<select asp-for="SelectedProcess" asp-items="Model.ProcessList" name="SelectedValue" style="width:250px;max-width:250px;">
<option value="">Select Process</option>
</select>
</div>
<input type="submit" value="Load" class="btn btn-primary" /> |
</p>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.secondAppealDetail[0].ApplicationNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.secondAppealDetail[0].NameofApplicant)
</th>
<th>
@Html.DisplayNameFor(model => model.secondAppealDetail[0].DetailAddress)
</th>
<th>
@Html.DisplayNameFor(model => model.secondAppealDetail[0].DepartmentDetails)
</th>
<th>
@Html.DisplayNameFor(model => model.secondAppealDetail[0].PIODetails)
</th>
<th>
@Html.DisplayNameFor(model => model.secondAppealDetail[0].RTIOutcome)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.secondAppealDetail)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ApplicationNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.NameofApplicant)
</td>
<td>
@Html.DisplayFor(modelItem => item.DetailAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.DepartmentDetails)
</td>
<td>
@Html.DisplayFor(modelItem => item.PIODetails)
</td>
<td>
@Html.DisplayFor(modelItem => item.RTIOutcome)
</td>
<td>
<a asp-page="./Index" asp-route-id="@item.SecondAppealId">Details</a>
</td>
</tr>
}
</tbody>
</table>
Однако ничего не отображается, когда я пытаюсь запустить тот же обработчик с помощью JQUERY.
public async Task<PageResult> OnGetAsync(Guid? id, int? currentProcess)
{
//Get users Role.
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId
if (userId == null)
{
Message = "Some Problem!! (UE)";
return Page();
}
IList<string> roles = await GetRoles(userId);
if (roles == null)
{
Message = "Some Problem!! (RE)";
return Page();
}
List<Process> processesForThisRole = _context.Processes.Where(a => roles.Contains(a.RoleId)).ToList();
ProcessList = new SelectList(processesForThisRole, nameof(Process.ProcessID), nameof(Process.Description));
//Subsequent calls
var result = _context.SecondAppeal
.Where(me => me.CurrentProcess == currentProcess)
.ToList()
.Select(me =>
new InputModel()
{
SecondAppealId = me.SeconAppealId,
NameofApplicant = me.NameofApplicant,
ApplicationNumber = me.ApplicationNumber,
PIODetails = string.Join(" ; ", me.CPIOName, me.CPIOAddress),
RTIOutcome = string.Join(" ", " RTI Date ", me.RTIDate.ToShortDateString(), ", RTI Response: ", me.CPIOResponse.ToString()),
DetailAddress = string.Join(" ; ", me.Address, me.TownVillage, me.StateName, me.DistrictName, me.PinCode),
DepartmentDetails = string.Join(" , ", me.DepartmentName, me.PublicAuthority),
}
);
secondAppealDetail = result.ToList();
return Page();
}
Соответствующий скрипт
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
$(function () {
$("#SelectedProcess").on("change", function () {
var processCode = $(this).val();
alert(processCode);
$.ajax({
type: "GET",
url: "/ApplicationProcess",
contentType: "application/json",
dataType: "json",
data: { currentProcess: processCode },
success: function (response) {
},
failure: function (response) {
alert(response);
}
});
});
});
</script>
Что-то не так в моем коде или есть проблемы с пониманием Razor Pages.