I work on razor asp.net . I face issue drop down branch reset after return search result based on drop down branch .
I expect to still have same value I get the result based on it .
so why it cleared
Exactly I need selected branch i search by it still exist after click search result and return data .
<form id="FrmShelfLabelPrintrSetup" method="post" onsubmit="return validateForm() ">
<div class="row">
<div class="col-lg-12 col-12 row">
<div class="col-md-3 col-lg-2">
<label for="branch-selectlbl" style="margin-left:3px;font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">Print Server</label>
<select id="branch-select" asp-for="SelectedBranchId" name="selectbranchid" class="form-select" style="margin-left:3px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;" onchange="toggleButtonVisibility()">
<option value="0">--Select--</option>
@foreach (var branches in Model.branches)
{
<option value="@branches.iBranchCode">@branches.vBranchDesc</option>
}
</select>
</div>
<div class="col-md-3 col-lg-2">
<br>
<button id="Searchtxt" type="submit" name="searchButton" value="search" asp-page-handler="Search" style="width:100px;margin-top:7px;" class="btn btn-primary">Search</button>
</div>
</div>
</div>
</form>
</div>
public async Task<IActionResult> OnPostSearchAsync(string selectbranchid)
{
DataTable dtresults = _IAdcSupportService.ExecuteQuery(_IAdcSupportService.GetAllPrinters("Branch", selectbranchid));
if(dtresults.Rows.Count>0)
{
ViewData["SearchResults"] = dtresults;
}
DataTable dtbranches = _IAdcSupportService.GetBranchDetails();
branches = dtbranches.AsEnumerable()
.Select(row => new UC.ADC.Core.Entities.SQL.Branch
{
iBranchCode = (string)row["iBranchCode"],
vBranchDesc = row["vBranchDesc"].ToString()
})
.ToList();
return Page();
}
You can add a nullable attribute to the Branch
model to determine whether it is selected, and add [NotMapped]
to it to prevent it from matching in the database.
[NotMapped]
public bool? Selected { get; set; }
OnPostSearchAsync:
branches = dtbranches.AsEnumerable()
.Select(row => new UC.ADC.Core.Entities.SQL.Branch
{
iBranchCode = (string)row["iBranchCode"],
vBranchDesc = row["vBranchDesc"].ToString(),
// add this line
Selected = ((string)row["iBranchCode"] == selectbranchid)? true: null
})
.ToList();
And in your form:
@foreach (var branches in Model.branches)
{
string selected = null;
@if(branches.Selected == true)
{
selected = "selected";
}
<option selected=@selected value="@branches.iBranchCode">@branches.vBranchDesc</option>
}