使用数据库和 ajax asp.net 绑定单选按钮列表



Web 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":"单向"},{"ID":15,"journey_Name":"双向"},{"ID":16,"journey_Name":"多城市"}]">

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>

但是我无法将数据与单选按钮列表绑定

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

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

如果报告了任何特定错误,请更新帖子

最新更新