如何在使用后检查单选按钮?

问题描述:

我在我的网页上有6个单选按钮。当你点击其中一个按钮时,会显示总览。但是选中的单选按钮没有被选中。您会看到按钮显示为选中一段时间​​。但是和其他的一样,它将不再被检查。如何在使用后检查单选按钮?

我该如何保持它的检查?

的观点:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" > 
     <% using (Html.BeginForm("Overige_Statistieken", "Hitdossier", 

     FormMethod.Post, new { id = "frmStatistieken" })) %> 
      <% { %> 
       <table> 
        <tr> 
         <th class="hitlijst_editie">Selecteer overzicht:</th> 
        </tr> 
        <tr> 
         <td> 
         <%= Html.RadioButton("Overzicht", "1", new 
          { 
           @onclick = "document.getElementById('frmStatistieken').submit();" 
          }) %> Overzicht Alle Nummer 1 hits per week 
         </td> 
        </tr> 
        <tr> 
         <td> 
         <%= Html.RadioButton("Overzicht", "2", new 
          { 
           @onclick = "document.getElementById('frmStatistieken').submit();" 
          })%> Overzicht Alle Tips van de week 
         </td> 
        </tr> 
... 
     </table> 
     <br /> 
     <br /> 
     <div> 
      <%= Html.Action("Overige_StatistiekenLijst", new { AID_Overzicht = ViewBag.ID_Overzicht })%> <br /> 
     </div> 
    <% } %> 
</asp:Content> 

...这是控制器:

public ActionResult Overige_Statistieken(string AID_Overzicht = "1") 
{ 
    ViewBag.ID_Overzicht = AID_Overzicht; 
    return View(); 
} 

[HttpPost] 
public ActionResult Overige_Statistieken(FormCollection ACollection) 
{ 
    string sID_Overzicht = ACollection["Overzicht"].ToString(); 
    return Overige_Statistieken(sID_Overzicht); 
} 

public ActionResult Overige_StatistiekenLijst(string AID_Overzicht) 
{ 
    ViewBag.ID_Overzicht = AID_Overzicht; 

    ReadOverigeStatistieken(AID_Overzicht); 
    return View(_ListOverzichtModel); 
} 
+0

“你看到按钮显示为选中一段时间​​,但与其他选项一样,它将被再次取消选中。”点击它时是否显示为已选中?当您提交表单并刷新页面时,它可能会被取消选中吗?当按钮变为未选中时,页面上究竟发生了什么? – 2015-01-20 22:05:06

+0

所有单选按钮都有自己的概述。这工作正常。但这只是单选按钮的错误。所有按钮都未选中,并保持未选中状态。 – user1531040 2015-01-20 22:08:11

有一个重载方法,将允许您设置单选按钮的初始状态,使用isChecked参数(第三个参数)。

<%= Html.RadioButton("Overzicht", "1", (int)ViewBag.ID_Overzicht==1, new 
     { 
      @onclick = "document.getElementById('frmStatistieken').submit();" 
     }) %> 

以下内容已添加到您的代码中。对所有的单选按钮做同样的事情,注意你将检查的值(即1)。

ViewBag.ID_Overzicht==1 
+0

它看起来没问题。但我收到以下消息:错误'System.Web.Mvc.HtmlHelper '没有名为'RadioButton'的适用方法,但似乎具有通过该名称的扩展方法。扩展方法不能动态分派。考虑转换动态参数或调用扩展方法而不使用扩展方法语法。 – user1531040 2015-01-20 22:59:00

+0

这是问题所在。您必须将动态值转换为int或字符串。 – user1531040 2015-01-21 00:09:19