我试图在MVC4 RAZOR应用程序中单击按钮后将所选单选按钮的值回发给控制器。然而,我没有回到控制器中的实际预期值,只有0。
部分查看代码:
@model Blah.FeatureViewModel
<script type="text/javascript">
function OnContinueClick() {
$('#tabs').load('@Url.Action("Continue", "Home")', { SelectedFeature : Model.SelectedFeature });
}
</script>
@using (Html.BeginForm())
{
<div>
@foreach (var feature in Model.FeatureDictionary)
{
<p>
@Html.RadioButtonFor(x => x.SelectedFeature, feature) @feature.Value
</p>
}
<input type="button" name="continueButton" id="continueButton" value="Continue" onclick="OnContinueClick()" />
</div>
}
查看型号代码:
using System.Collections.Generic;
namespace Blah.Models
{
public class FeatureViewModel
{
public Dictionary<int, string> FeatureDictionary { get; set; }
public KeyValuePair<int, string> SelectedFeature { get; set; }
}
}
控制器代码:
[HttpPost]
public ActionResult Continue(FeatureViewModel featureViewModel)
{
//pass data to another controller
}
我想我可能需要让Jquery回发该值?
答案 0 :(得分:0)
传递给jQuery load
函数的值是在呈现视图时Model的值。要传递当前选定的值,您需要这样的内容:
$('#tabs').load('@Url.Action("Continue", "Home")',
{ SelectedFeature : $('[name="SelectedFeature"]').val() });
同样 Joel Etherton 说,您需要将操作类型从FeatureViewModel
更改为KeyValuePair<int, string>
。
我不确定
@Html.RadioButtonFor(x => x.SelectedFeature, feature)
由于功能是KeyValuePair<int, string>
,正在给你想要你想要的东西。你在尝试创建一个完全正确的单选按钮是什么?