2016-02-12 71 views
0

在我的MVC項目,目前我有下面的代碼在我看來單擊Html.ActionLink時,返回Html.BeginForm中所選單選按鈕的值?

編輯:我只是檢查和@using語句可以通過簡單的<form>被替換,它仍然似乎所有的工作一樣。但仍然有同樣的問題。

@using (Html.BeginForm("Index", "AccountsFinance", FormMethod.Post)) 
    { 
     <table cellspacing="10"> 
      <tr> 
       @foreach (string answer in Model.Answers) 
       { 
       <td><input type="radio" name="answer" value="@answer" /><span>@answer</span></td> 
       } 
      </tr> 
     </table> 
     <div id="footer"> 
      <input class="btn btn-success" direction="false" type="submit" name="Back" value="Back" /> 
      @if(Model.NextQuestion == 6) 
      { 
       <input type="submit" @Html.ActionLink("Next", "Index", "Result") /> 
      } 
      else 
      { 
       <a id="nextButton">@Html.ActionLink("Next", "Index", new { questionId = Model.NextQuestion })</a> 
      } 
     </div> 
    } 

我所需要的是,當用戶點擊@Html.ActionLink...(其具有id="nextButton),用於形式發送到控制器的方法,所選擇的單選按鈕的值。這是在我的控制器代碼:我之前已經實現了這個

public ActionResult Index(string questionId) 
    { 
     int qId = int.Parse(questionId); 

     using (S3WEntities1 ent = new S3WEntities1()) 
     { 
      afqList.Question = ent.Questions.Where(w => w.QuQuestionId == qId).Select(s => s.QuQuestion).FirstOrDefault().ToString(); 
      afqList.Answers = ent.Answers.Where(w => w.AnsQuestionId == qId).Select(s => s.AnsAnswer).ToList(); 
      afqList.NextQuestion = qId + 1; 
     } 

     return View("Index", afqList); 
    } 

,但使用@Html.BeginForm元素時,沒有等那麼,如何選擇的單選按鈕的值傳遞給控制器​​,當@Html.ActionLink("Next"...被點擊?提前致謝!


編輯2: 另一個類似的問題也可能解決我的問題,我將如何選擇的單選按鈕的值分配給在模型創建的變量,類似於questionId = Model.NextQuestion。如果這個問題得到解答,我可以簡單地按照我所做的Model.NextQuestion和所選的單選按鈕值完成相同的操作。

+0

嗨,我的回答對你有幫助嗎? –

回答

0

如果您想要表單要發佈內容,您需要提交表單,錨定標記不會進行表單提交。

你需要Ajax來做到這一點(有時,可能涉及安裝必要的金塊包不顯眼的AJAX和jQuery等)

$('#nextButton').click(function(){ 

    $.ajax({ 
       type: "POST", 
       url: '@Url.Action("Next", "Index")', 
       data: { 
       selectedQuestionId : $("input[type='radio']:checked").value, 
       nextQuestionId: Model.NextQuestion 
         } 
      }); 
}); 

編碼愉快!