gpt4 book ai didi

javascript - 如何从单选按钮中提取值并通过 mvc4 中的 ajax.actionlink 将其传递给操作?

转载 作者:行者123 更新时间:2023-12-03 08:40:19 25 4
gpt4 key购买 nike

我有一个问题和选项列表,这些选项在 View 中显示为单选按钮。我有可以通过 Ajax.ActionLink() 访问的上一个和下一个操作方法,在发送时,我想提取所选单选按钮的值。有人可以建议一个好的实现吗?

我的观点:

<div class="techquestions" id="techquestiondetail">
<div>
<h4>Q.</h4><h5>@Html.DisplayFor(model => TechQ.Question)</h5><br />
<div>Options:</div><br />
<div class="tech_answer_radiobutton" id="tech_radiobuttongroup">
@Html.RadioButton("OptionSelected", TechQ.OptionA)<span> @TechQ.OptionA.ToString()</span>
@Html.RadioButton("OptionSelected", TechQ.OptionB)<span> @TechQ.OptionB.ToString()</span>
@Html.RadioButton("OptionSelected", TechQ.OptionC)<span> @TechQ.OptionC</span>
</div>
</div>
</div>
@Ajax.ActionLink("Previous", "Previous", new { id = Convert.ToInt32(Session["Id"]) }, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "parti", InsertionMode = InsertionMode.Replace })
@Ajax.ActionLink("Next", "Next", new { id = Convert.ToInt32(Session["Id"]) }, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "parti", InsertionMode = InsertionMode.Replace })

Controller :

 public ActionResult Next(int id, string answer) //the optionselected should come here
{

}

我们可以像这样生成 ajax.actionlink 的 url 吗:AwesomeController/DoSomething?foo=foo1&bar=bar1?

最佳答案

不要使用Ajax.ActionLink()。请改用 jquery 函数(这样可以节省包含 jquery-unobtrusive-ajax.js 带来的不必要的额外开销)。修改 html 链接到

<a href="#" id="previous">Previous</a>
<a href="#" id="next">Next</a>

并添加以下脚本

var id = '@Convert.ToInt32(Session["Id"])'
var target = $('#parti');
$('#previous').click(function() {
var url = '@Url.Action("Previous")';
var answer = $('#OptionSelected').val();
target.load(url, { id: id, answer: answer });
}
$('#next').click(function() {
var url = '@Url.Action("Next")';
var answer = $('#OptionSelected').val();
target.load(url, { id: id, answer: answer });
}

关于javascript - 如何从单选按钮中提取值并通过 mvc4 中的 ajax.actionlink 将其传递给操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054583/

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