razor page 自動bind
自動bind
public IActionResult OnPostModuleOnchange([FromBody] PostData post)
{
return new JsonResult(MssqlConn.GetToolIDInfo(post.fab, post.module));
}
[BindProperties]
public class PostData
{
public string fab { get; set; }
public string module { get; set; }
public string tool { get; set; }
}
ajax 傳下拉式選單的value
$('#MODULE').change(function () {
var fabValue = $('#FAB').find(":selected").val();
var selectedvalue = $(this).find(":selected").val();
$.ajax({
type: "POST",
url: './SPECAlarmRTTCSetting?handler=ModuleOnchange',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN", $('input:hidden[name="__RequestVerificationToken"]').val());
},//razor page 需要驗證
data: JSON.stringify({
fab: fabValue,
module: selectedvalue
}),
contentType: 'application/json; charset=utf-8',
dataType: "json"
}).done(function (data) {
var options = '';
for (var i = 0; i < data.length; i++) {
options += '<option value="' + data[i].toolid + '">' + data[i].toolid + '</option>';
}
$("select#TOOL").html(options);
console.log(data);
})
});
或是
public IActionResult OnPostFabOnchange(string fab)
{
return new JsonResult(MssqlConn.GetModuleNameInfo(fab));
}
ajax
$('#FAB').change(function () {
var selectedvalue = $(this).find(":selected").val();
var data = new FormData();
data.append('fab', selectedvalue);
//alert(selectedvalue);
$.ajax({
type: "POST",
url: './SPECAlarmRTTCSetting?handler=FabOnchange',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN", $('input:hidden[name="__RequestVerificationToken"]').val());
},
processData: false,
contentType: false,
data: data,
dataType: "json"
}).done(function (data) {
var options = '';
for (var i = 0; i < data.length; i++) {
options += '<option value="' + data[i].modulename + '">' + data[i].modulename + '</option>';
}
$("select#MODULE").html(options);
console.log(data);
})
});
手刻取出ajax post 值
string module = "";
MemoryStream stream = new MemoryStream();
Request.Body.CopyTo(stream);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
string requestBody = reader.ReadToEnd();
if (requestBody.Length > 0)
{
var obj = JsonConvert.DeserializeObject<PostData>(requestBody);
if (obj != null)
{
fab = obj.fab;
module = obj.module;
}
}
}