【问题标题】:Asp.net mvc radiobutton retrieve selected dataAsp.net mvc 单选按钮检索选定的数据
【发布时间】:2014-01-07 14:48:50
【问题描述】:

我正在使用带有 razor 和 javascript 的 ASP.net MVC4。

我加载我的视图如下: 当Retrieve data from database不检查radiobutton的值时,radiobutton是空的,我该如何解决这个问题? Textbox中的值都是回收的radiobutton号。

    <table>
    <tr>
        <th>Parte Frontal</th>
        <th>Cant.</th>
        <th>Bueno</th>
        <th>Regular</th>
        <th>Deficiente</th>
        <th>Observaciones</th>
    </tr>      
    @{int numitem = 0;} 
    @for (var i = 0; i < ViewBag.DatosExtFrontal.Count; i++)
    {                                          
        <tr>            
        <td>@ViewBag.DatosExtFrontal[i].cprp_descripcion</td>
        <td>@Html.TextBox(string.Format("txtpieza{0}", numitem), (int)ViewBag.DatosExtFrontal[i].piv_cantidad, new { id = string.Format("txtpieza{0}", ViewBag.DatosExtFrontal[i].cprp_idpartepre.ToString()), style = "width:25px;text-align:right;", onkeyup = "checkInt(this)", sololectura = false })</td>       
        <td class="td-center">               
            @Html.RadioButton(String.Format("rbtestado{0}", numitem), "B", new { id = string.Format("B{0}", ViewBag.DatosExtFrontal[i].cprp_idpartepre.ToString()), sololectura = false })                                
        </td>
        <td class="td-center">
            @Html.RadioButton(String.Format("rbtestado{0}", numitem), "R", new { id = string.Format("R{0}", ViewBag.DatosExtFrontal[i].cprp_idpartepre.ToString()), sololectura = false })                                
        </td>
        <td class="td-center">
            @Html.RadioButton(String.Format("rbtestado{0}", numitem), "D", new { id = string.Format("D{0}", ViewBag.DatosExtFrontal[i].cprp_idpartepre.ToString()), sololectura = false })                                
        </td>                
        <td>@Html.TextBox(string.Format("txtobservacion{0}", numitem), (string)ViewBag.DatosExtFrontal[i].piv_observacion, new { id = string.Format("txtobservacion{0}", ViewBag.DatosExtFrontal[i].cprp_idpartepre.ToString()), style = "width:150px;", sololectura = false })</td>
        <td>
        @{                  
            string IdPartePre = String.Format("{0}", numitem);
            string PartePre = String.Format("{0}", (int)ViewBag.DatosExtFrontal[i].cprp_idpartepre);
            numitem++;             
        }  
        <input id="@IdPartePre" type="hidden" name="@PartePre" value="@PartePre"/>
        </td>
        </tr>     

    } 
    </table>

【问题讨论】:

  • 帮助器将值与您的模型联系起来。将单选按钮更改为 @Html.RadioButtonFor
  • 检查您的控制器并使用 viewbug 显示控制器操作
  • @MattBodily 如果我使用 Html.RadioButtonFor 它,这将为我的所有单选按钮生成相同的名称,我无法选择多个单选按钮
  • @user1956570 我的控制器返回带有当前信息的 DatosExtFrontal 因为加载文本框中的值

标签: c# javascript asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

如果您不能使用 for 助手将其绑定到您的模型,那么您将需要在该字段上放置一个唯一名称,然后您可以使用控制器获取值

Request.Form["RadioName"].ToString()

【讨论】:

  • 但唯一的名称是:@Html.RadioButton(String.Format("rbtestado{0}", numitem), "B", new { id = string.Format("B{0} ", ViewBag.DatosExtFrontal[i].cprp_idpartepre.ToString()), sololectura = false })
  • 看看单选按钮在视图上是如何呈现的,并确保请求中的名称匹配,你应该很好
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 2021-07-17
  • 2018-04-15
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
相关资源
最近更新 更多