【问题标题】:Bind Radiobuttonlist using database and ajax asp.net使用数据库和 ajax asp.net 绑定 Radiobuttonlist
【发布时间】:2018-05-07 21:19:14
【问题描述】:

网络 API

[HttpPost]  //Get Vendor Master
    public HttpResponseMessage GetVendorMaster(dynamic inXml)
    {
        string typeids = inXml.inXml;
        int TypeID = Convert.ToInt32(typeids);
        return new HttpResponseMessage()
        {

            Content = new StringContent(clsCommonLib.DataTableToJSON(MasterDetailsBO.GetVendorMaster(TypeID)), System.Text.Encoding.UTF8, "application/json")
        };
    }

数据: "[{"ID":14,"journey_Name":"One Way"},{"ID":15,"journey_Name":"Two Way"},{"ID":16,"journey_Name":"Multi City "}]"

Ajax 方法:

function bind()
{

 $.ajax({
    type: "POST",
    url: vUrlWithClass,
    contentType: "application/json;",
    data: param,
    async: false,
    dataType: "json",
    success: function (data) {

        var result = $.parseJSON(data.d)
        console.log(data)        

            $.each(result, function (index, list) {

                    var rdb = "<tr><td><input id=rb" + this['Text'] + "  type='radio' name='rbCategories' value=" + this['Text'] + " /><label for=lbl" + this['Text'] + ">" + this['Text'] + "</label></td></tr>";
                    table.append(rdb);
                //$("#cphDashboard_ddlJourneytype").append($("<option></option>").val(list.ID).html(list.journey_Name));

            })

    },
    error: function (result) {
        console.log(result)
        alert(result)
    }
});
}
}

ASPX 代码:

<asp:RadioButtonList ID="rdbJourneyTypeAir" runat="server" RepeatDirection="Horizontal" CssClass="FormatRadioButtonList">
</asp:RadioButtonList>

但我无法使用单选按钮列表绑定数据

【问题讨论】:

  • 您混淆了服务器端和客户端。 RadioButtonList 是服务器端。 ASP.NET 4.0 能够进行客户端数据绑定 ASP.NET AJAX 4.0 中的数据绑定 (msdn.microsoft.com/en-us/magazine/ee309508.aspx) 如果您不使用 .4.0,那么只需遍历数组并附加 html 元素即可到容器
  • 您的方法以 Get 为前缀,并且您正在创建 Post,命名差异令人困惑。请更新以符合设计指南
  • 绑定到单选按钮列表时遇到什么错误
  • 无法将单选按钮列表与数据库绑定

标签: asp.net ajax radiobuttonlist


【解决方案1】:

从示例代码中可以看出:-

  1. http 动词 (Post) 和方法名称 (Get...) 不匹配。您需要了解何时使用 get 与 post 获取资源
  2. this["Text"] 的计算结果是什么?理想情况下,它应该是一个迭代器,为转发器控件启用动态 id。看起来它们对于所有单选按钮都是一样的。
  3. 如果 'text' 计算结果为字符串,请确保其中没有空格或特殊字符,这可能会阻止生成有效的 Id
  4. 在成功处理程序中放置一个调试器以查看“列表”的值。您可能需要展开它以识别返回的结构。

如果有任何具体的错误报告,请更新帖子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多