Question: How Can I Get The BranchValue From DropDown Which Is In VIEW Mentioned Below.
And Pass It To The [Http Post] In Controller To Fetch Another JSON Data.
I Have a Partial View Name "_FilterBranch"
@model FilterBranch
@Html.DropDownListFor(model => model.BRNO, ViewBag.DropdownDataBranch as List<SelectListItem>, "Select Branch", new { @class = "js-example-basic-single col-sm-12"})
@section scripts {
<link rel="stylesheet" type="text/css" href="~/assets/css/vendors/select2.css">
<script src="~/assets/js/select2/select2.full.min.js"></script>
<script src="~/assets/js/select2/select2-custom.js"></script>
}
Then Have a Model Name "FilterBranch"
namespace EmployeeAffairs.Models
{
public class FilterBranch
{
public string BRNO { get; set; }
public string BRNAME { get; set; }
}
}
Then Have a Class Name: GetBranchList
using EmployeeAffairs.Models;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using System.Net.Http.Headers;
namespace EmployeeAffairs.Controllers
{
public static class FillData
{
public static List<SelectListItem> DropDownBranchList { get; set; } = new List<SelectListItem>();
public static async Task initFunc(HttpContext httpContext)
{
DropDownBranchList = await GetDropDownBranchName(httpContext);
}
public static async Task<List<SelectListItem>> GetDropDownBranchName(HttpContext httpContext)
{
var accessToken = httpContext.Session.GetString("AccessToken");
var userId = httpContext.Session.GetString("UserId");
var filterType = "2";
var lang = "0";
var url = $"http://210.255.200.205:830/api/Fill/Fill?filterType={filterType}&lang={lang}&userId={userId}";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<List<FilterBranch>>(json);
return data.Select(item => new SelectListItem
{
Value = item.BRNO,
Text = item.BRNAME
}).ToList();
}
else
{
throw new Exception($"Failed to retrieve data: {response.StatusCode}");
}
}
}
}
}
Now I Have a View Name "AttendanceReportByBranch"
<div class="card-body">
@using (Html.BeginForm("AttendanceReportByBranch", "Attendance", FormMethod.Post))
{
<div class="row mb-4 align-items-end">
<div class="col-md-3">
<div class="m-form__group">
<label class="form-label">الوحدة</label>
<select name="ddlBranch" class="js-example-basic-single col-sm-12">
<option value="">--Select Branch--</option>
@foreach (var item in ViewBag.DropdownDataBranch)
{
<option value="@item.Value">@item.Text</option>
}
</select>
</div>
</div>
<div class="col-md-2">
<label class="col-form-label pt-0">تاريخ من</label>
<input class="form-control digits" required="required" type="date" name="dtFrom">
</div>
<div class="col-md-2">
<label class="col-form-label pt-0">تاريخ إلى</label>
<input class="form-control digits" required="required" type="date" name="dtTo">
</div>
<div class="col-md-1">
<button class="btn btn-group-sm btn-block btn-primary" type="submit">
class="fa fa-search">
</button>
</div>
</div>
}
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="display" id="basic-1">
<thead>
<tr>
<th>EMPNO</th>
<th>EMPNAME</th>
<th>NODAY</th>
<th>OFFAY</th>
<th>LEV</th>
<th>ABST</th>
<th>PRESENT</th>
<th>OVTM</th>
<th>LATE</th>
<th>ELATE</th>
<th>PERMIS</th>
<th>TTLATE</th>
<th>PERMTOT</th>
<th>WORKED_MIN</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
@foreach (var ModelAttendanceReportByBranch in Model)
{
<tr>
<td>@ModelAttendanceReportByBranch.EMPNO</td>
<td>@ModelAttendanceReportByBranch.EMPNAME</td>
<td>@ModelAttendanceReportByBranch.NODAY</td>
<td>@ModelAttendanceReportByBranch.OFFAY</td>
<td>@ModelAttendanceReportByBranch.LEV</td>
<td>@ModelAttendanceReportByBranch.ABST</td>
<td>@ModelAttendanceReportByBranch.PRESENT</td>
<td>@ModelAttendanceReportByBranch.OVTM</td>
<td>@ModelAttendanceReportByBranch.LATE</td>
<td>@ModelAttendanceReportByBranch.ELATE</td>
<td>@ModelAttendanceReportByBranch.PERMIS</td>
<td>@ModelAttendanceReportByBranch.TTLATE</td>
<td>@ModelAttendanceReportByBranch.PERMTOT</td>
<td>@ModelAttendanceReportByBranch.WORKED_MIN</td>
</tr>
}
}
</tbody>
</table>
</div>
</div>
</div>
</div>
And Here Is The Controller : "AttendanceController"
public async Task<IActionResult> AttendanceReportByBranch()
{
if (FillData.DropDownBranchList == null || FillData.DropDownBranchList.Count == 0)
{
await FillData.initFunc(HttpContext);
}
ViewBag.DropdownDataBranch = FillData.DropDownBranchList;
ViewBag.SelectedBranch = "";
return View();
}
[HttpPost]
public async Task<IActionResult> AttendanceReportByBranch(string dtFrom, string dtTo, string ddlBranch)
{
var BranchNo = ddlBranch;
var dateFrom = dtFrom;
var dateTo = dtTo;
var apiUrlAttendance = "http://210.255.200.205:830/api/Attendance/AttendanceReportByBranch";
var accessToken = HttpContext.Session.GetString("AccessToken");
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var responseAttendance = await _httpClient.PostAsync(apiUrlAttendance, new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "BranchNumber", BranchNo.ToString() },
{ "DateFrom", dateFrom },
{ "DateTo", dateTo }
}));
if (responseAttendance.IsSuccessStatusCode)
{
var jsonContent = await responseAttendance.Content.ReadAsStringAsync();
var attbyBranch = JsonConvert.DeserializeObject<List<ModelAttendanceReportByBranch>>(jsonContent);
return View(attbyBranch);
}
else
{
return View();
}
}
What I have tried:
Can Anyone Help Me In This ? Please.