radiobuttonfor から取得した ID を MainPage コントローラーに渡す方法 - Edit1 actionresult in View. どんな助けでも大歓迎です!ありがとう!
// for ループで表示してコンテンツとラジオボタンを表示する
@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>
// どのラジオボタン 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);
}