2012-10-26 29 views
0

如何将从radiobuttonfor检索到的ID传递给MainPage控制器 - View中的Edit1 actionresult。任何帮助将不胜感激!谢谢!如何使用jQuery将ID传递给MVC ActionResult?

//浏览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); 
      } 
+0

我需要澄清你要做的事情...我不断编辑我的答案B/C我不是很确定你想要完成什么。您是否试图根据所选的单选按钮导航到编辑表单? –

+0

我试图让用户选择一个单选按钮,然后单击提交按钮,它会进入该方法与ID并做必要的事情,并最终返回一个页面的ID。但现在的问题是ID不起作用。 – xJedx

+0

将视图的相关部分添加到问题中。 –

回答

0

试试这个:

http://api.jquery.com/jQuery.post/

或者您的ID的地方,一些隐藏的文本字段,并触发点击提交按钮(如果你有一个页面上的网页形式)

+0

我已经尝试过,它确实进入了方法,但id始终是0.但是,每当我使用警报调试有价值。 – xJedx

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. 

}); 
+0

如果我将它作为静态值,ur方法将起作用。但如何获得单选按钮的ID如果它是动态的?就像用户选择1单选按钮并单击该按钮时一样。 – xJedx

+0

问题是与id,它不会工作,不知道为什么,但如果我将它设置为静态值,它的工作原理。 – xJedx

+0

@ Html.RadioButtonFor(modelItem => item.SurveyId,new {@id = item.SurveyId})这个部分有什么问题吗? – xJedx

0

这哪里是提交按钮,你说的?是#edit?如果您想要达到POST操作,您需要让它发布表单。此外,它不会绑定到int id在你的行动,除非你更改收音机像@Html.RadioButton("id", item.SurveyId)

+0

是啊它是一个后按钮,我duno y stackoverflow不允许我编辑,不断提示我一些错误 – xJedx

+0

确定编辑,我试过@ Html.RadioButton(“id”,item.SurveyId),但item.SurveyId应该b int?但它是字符串 – xJedx

相关问题