I work on asp.net core razor page model .I face issue i can't store result of select option ajax request that fill order type
to fill it after serach reset submit done
because after search button click order type select option reset
so i need to fill select option again of data that have before submit or before reset
so I get data list fill select option order type success on function RelatedOrderType with
reprintModel.OrderTypes = orderTypes;
How to get result or store result of reprintModel.OrderTypes = orderTypes; on funcion OnPost after search submit done
meaning how to get result of ajax request onPost function after search submit finish
$('#orderType-select').prepend('');
my code details as below
OrderType
--Select--
@foreach (var ordertype in Model.reprintModel.OrderTypes)
{
@ordertype.OrderTypes
}
$('#totimeselect').change(function (event) {
//event.preventDefault();
var DateLogged = $("#statusDateId").val();
var branchId = $("#branch-select").val();
var FromTime = $("#txtFromTime").val();
var ToTime = $("#totimeselect").val();
console.log(branchId);
if (DateLogged) {
console.log(DateLogged);
$.ajax({
url: '?handler=RelatedOrderType',
type: "GET",
dataType: "json",
data: { branchId: branchId, DateLogged: DateLogged, FromTime: FromTime, ToTime: ToTime },
success: function (data) {
//console.log(data);
$('#orderType-select').empty();
$.each(data, function (i, item) {
console.log(item);
$('#orderType-select').append($('', {
value: item.orderTypes,
text: item.orderTypes
}));
});
// Add a default option
$('#orderType-select').prepend('-- Select Order Type --');
}
});
}
});public JsonResult OnGetRelatedOrderType(string branchId, string DateLogged,string FromTime,string ToTime)
{
string eventDatefromtime = Convert.ToDateTime(FromTime).ToString("HH:mm");
string eventDatetotime = Convert.ToDateTime(FromTime).AddMinutes(Convert.ToInt16(ToTime)).ToString("HH:mm");
DataTable dtordertype = _IAdcSupportService.GetOrderTypes(branchId, DateLogged, eventDatefromtime, eventDatetotime);
List orderTypes = dtordertype.AsEnumerable()
.Select(row => new OrderTypePrinterServer
{
// Map the values from the DataRow to the properties of the PrintServer object
OrderTypes = row["Doc_type"].ToString()
})
.ToList();
reprintModel.OrderTypes = orderTypes;
return new JsonResult(orderTypes);
}Now I click on search button to display data
so OnPost Function will fire
public async Task OnPost(IFormCollection formcollection)
{
//after search data and data display select option order type will reset so I will fill again
reprintModel.OrderTypes = orderTypes;
}
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jul 16, 2023, 2:06 AM EDT
Hi ahmad
Create a hidden input field in your Razor Page's form to hold the serialized JSON data representing the
reprintModel.OrderTypes. Add this field within thetag:in your AJAX success function, serialize the
dataresponse and set the value of the hidden input field with the serialized JSON:success: function (data) { $('#orderType-select').empty(); $.each(data, function (i, item) { $('#orderType-select').append($('', { value: item.orderTypes, text: item.orderTypes })); }); $('#orderType-select').prepend('-- Select Order Type --'); $('#orderTypesData').val(JSON.stringify(data)); }In the
OnPostmethod, retrieve the serialized JSON data from theorderTypesDataform field, deserialize it back into a list ofOrderTypePrinterServerobjects, and assign it toreprintModel.OrderTypes:public async Task OnPost(IFormCollection formcollection) { // Retrieve the serialized JSON data var orderTypesJson = formcollection["orderTypesData"]; // Deserialize the JSON into a list of OrderTypePrinterServer objects var orderTypes = JsonConvert.DeserializeObject>(orderTypesJson); // Assign the deserialized orderTypes to reprintModel.OrderTypes reprintModel.OrderTypes = orderTypes; // ... Rest of your code ... }If this helped kindly accept ythe anser.