gpt4 book ai didi

javascript - 如果可选参数概念不起作用怎么办?

转载 作者:行者123 更新时间:2023-11-29 23:19:02 25 4
gpt4 key购买 nike

我有一个 JS 函数,它根据所选的单选按钮从文本框中获取值。

示例:如果 RadioButton No 被选中,则值取自 TextBox A,否则如果 RadioButton Yes 被选中,值取自文本框 B。以下脚本在我看来

$('#btnVolunteerSaveBtn').on('click', function() { // on click of save button
if (document.getElementById('RadioNo').checked) { //ID of radio button NO
var checking = $('#Donation').val(); //ID of textbox from where the value is to be taken if RadioButton No is selected
if (checking == "") {
//if nothing is entered, stop from saving in DB
} else {
x = $('#Donation').val(); //ID of textbox from where the value is to be taken if RadioButton No is selected
$.ajax({
url: '@Url.Action("DonationValue","VolunteerInfo")',
data: {
name: x
},
type: "POST"
});
}
} else {
x = $('#GetNames').val(); //ID of textbox from where the value is to be taken if RadioButton Yes is selected
$.ajax({
url: '@Url.Action("DonationValue","VolunteerInfo")',
data: {
name: x
},
type: "POST"
});
}
});

到这里它似乎工作正常。现在来到 Controller ,我有一个函数 DonationValue

我的问题:

  1. 如何传递上面的name参数?
  2. 如果id为#Donation的TextBox中没有任何内容,我该如何停止从将表格保存在数据库中?

我的尝试:

我试过

public string DonationValue(string name = null)
{
return name; //Trying to pass this value above
}

这没有帮助。它解决了错误,但传递的值始终为空。我还尝试了其他一些方法,但都没有帮助。

已编辑:

[HttpPost]
public ActionResult AddVolunteer(VolunteerInfo viewModel)
{
if (!ModelState.IsValid)
{
return View("AddVolunteer", viewModel);
}

var volunteer = new VolunteerInfo()
{
Name = viewModel.Name,
BirthdayDateTime = viewModel.BirthdayDateTime,
Address = viewModel.Address,
PhoneNumber = viewModel.PhoneNumber,
EmailAddress = viewModel.EmailAddress,
OccasionsID = viewModel.OccasionsID,
DonationForWhom = _DonationValue
};

if (!string.IsNullOrEmpty(volunteer.DonationForWhom))
{
_context.VolunteerInfos.Add(volunteer);
_context.SaveChanges();
return RedirectToAction("Index", "Home");
}

return //something to save state so that user doesnt have to enter all the values again
}

[HttpPost]
public void DonationValue(string name)
{
_DonationValue = name;
}

最佳答案

@黛西希普顿。这是更好的解决方案吗?

<script>
$(function() {
$('#btnVolunteerSaveBtn').on('click', function() { // on click of save button
debugger;
if (document.getElementById('RadioNo').checked) { //ID of radio button NO
var checking = $('#Donation').val(); //ID of textbox from where the value is to be taken if RadioButton No is selected
if (checking == "") {
//if nothing is entered, stop from saving in DB
}
else {
var x = $('#Donation').val(); //ID of textbox from where the value is to be taken if RadioButton No is selected
var jsonObject = {
"textValue": x,
"isRadioSelected": "true" // show the radio is selected
};

$.ajax({
url: '@Url.Action("AddVolunteer", "VolunteerInfo")',
data: JSON.stringify(jsonObject),
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
alert(response);
}
});
}
}
else {
var jsonObject2 = {
"textValue": $('#GetNames').val(),
"isRadioSelected": "false" // show the radio is not selected
};

$.ajax({
url: '@Url.Action("AddVolunteer", "VolunteerInfo")',
data: JSON.stringify(jsonObject2),
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
alert(response);
}
});
}

});
})
</script>

在我的 Controller 中:

[HttpPost]
public ActionResult AddVolunteer(VolunteerInfo volunteerInfo)
{
if (volunteerInfo.isRadioSelected)
{
//something
}
else
{
//something
return View();
}

关于javascript - 如果可选参数概念不起作用怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51453845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com