如何将从radiobuttonfor检索到的id传递给MainPage控制器 - View中的Edit1 actionresult。任何帮助将不胜感激!谢谢!
//在for循环中查看以显示内容和radiobutton
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.SurveyTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateCreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateClosing)
</td>
<td>
@Html.DisplayFor(modelItem => item.User)
</td>
<td>
@Html.RadioButtonFor(modelItem => item.SurveyId, new {id = item.SurveyId})
</td>
</tr>
}
<button type="submit" class="btn btn-primary" id="edit">Edit</button>
//用于检测选择了哪个radiobutton id的按钮的脚本并提交
<script>
$('#edit').click(function () {
var id = $('input[type=radio]:checked').val();
alert(id);
});
</script>
//获取id并获取surveyid的控制器
[HttpPost]
public ActionResult Edit1(int id=0)
{
survey survey = db.surveys.Single(s => s.SurveyId == id);
if (survey == null)
{
return HttpNotFound();
}
return View(survey);
}
答案 0 :(得分:0)
答案 1 :(得分:0)
当用户根据单选按钮列表设置的特定ID单击编辑时,您似乎正在尝试更改窗口的位置。
我认为你可以做这样的事情:
$('#edit').click(function () {
var id = $('input[type=radio]:checked').val();
window.location.href='/MainPage/Edit1/'+id;
//window.location.href='/MainPage/Edit1/?id='+id; //if {controller}/{action}/{id} isn't setup in the routing class.
});
答案 2 :(得分:0)
您说的这个提交按钮在哪里?是#edit
吗?如果要进行POST操作,则需要让它对表单进行POST。此外,除非您将收音机更改为int id
@Html.RadioButton("id", item.SurveyId)