在 AJAX 通话中选择枚举项目
Selecting Enum items on AJAX call
我正在处理要查看 returns JSON 数据的操作结果,然后通过 AJAX 调用
加载到 textFields
操作:
public ActionResult loadInf(string custm)
{
int smclientbranchid = Convert.ToInt32(Session["smclientbranchid"]);
var query = (from parent in db.Customer
join child in db.CustomerAddress on parent.CustomerId equals child.CustomerId
where parent.SMClientBranchId == smclientbranchid && parent.NIC == custm
select new SalesVM
{
Indicator = parent.Indicator,
//code removed
}).ToList();
return Json(query);
}
在视图中:
@Html.DropDownListFor(model => model.Indicator,
new SelectList(Enum.GetValues(typeof(ColorTypes))),
"<Select>",
new { @class = "form-control", id ="Indicator" })
<script type="text/javascript">
$(document).ready(function () {
$("#btnSearchCus").click(function () {
var custm = $('#custm').val();
$.ajax({
cashe: 'false',
type: "POST",
data: { "custm": custm },
url: '@Url.Action("LoadCustomerInfo", "Sales")',
dataType: 'json', // add this line
"success": function (data) {
if (data != null) {
var vdata = data;
$("#Indicator").val(vdata[0].Indicator);
//code removed
}
}
});
});
});
</script>
除了 "Indicator" 字段是枚举类型之外,我正在正确获取数据并正确加载。
我怎样才能 select 从我得到的数据中得到枚举列表项。
例如:
0,1,2,3 index order
如果您检索字符串变量 nm (0,1,2,3...) - 最好将类型更改为 int 并尝试将您的整数变量转换为您拥有的 Enum
类型。
public ActionResult loadInf(int nm)
{
ColorTypes enumValue = (ColorTypes) nm;
.......
你可以看看这篇文章的细节:http://www.jarloo.com/convert-an-int-or-string-to-an-enum/
您需要针对 select 列表的所有 option
值设置 Value
属性。
将以下下拉框用于 select 按值的文本表示形式:
@Html.DropDownListFor(model => model.Indicator, Enum.GetValues(typeof(ColorTypes)).Cast<ColorTypes>().Select(x => new SelectListItem { Text = x.ToString(), Value = x.ToString() }), new { @class = "form-control", id = "Indicator" })
或使用以下内容使其 select 达到 integer
值:
@Html.DropDownListFor(model => model.Indicator, Enum.GetValues(typeof(ColorTypes)).Cast<ColorTypes>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }), new { @class = "form-control", id = "Indicator" })
这将使您的 .Val()
jQuery 代码成为 select 正确的代码。
我正在处理要查看 returns JSON 数据的操作结果,然后通过 AJAX 调用
加载到 textFields操作:
public ActionResult loadInf(string custm)
{
int smclientbranchid = Convert.ToInt32(Session["smclientbranchid"]);
var query = (from parent in db.Customer
join child in db.CustomerAddress on parent.CustomerId equals child.CustomerId
where parent.SMClientBranchId == smclientbranchid && parent.NIC == custm
select new SalesVM
{
Indicator = parent.Indicator,
//code removed
}).ToList();
return Json(query);
}
在视图中:
@Html.DropDownListFor(model => model.Indicator,
new SelectList(Enum.GetValues(typeof(ColorTypes))),
"<Select>",
new { @class = "form-control", id ="Indicator" })
<script type="text/javascript">
$(document).ready(function () {
$("#btnSearchCus").click(function () {
var custm = $('#custm').val();
$.ajax({
cashe: 'false',
type: "POST",
data: { "custm": custm },
url: '@Url.Action("LoadCustomerInfo", "Sales")',
dataType: 'json', // add this line
"success": function (data) {
if (data != null) {
var vdata = data;
$("#Indicator").val(vdata[0].Indicator);
//code removed
}
}
});
});
});
</script>
除了 "Indicator" 字段是枚举类型之外,我正在正确获取数据并正确加载。
我怎样才能 select 从我得到的数据中得到枚举列表项。
例如:
0,1,2,3 index order
如果您检索字符串变量 nm (0,1,2,3...) - 最好将类型更改为 int 并尝试将您的整数变量转换为您拥有的 Enum
类型。
public ActionResult loadInf(int nm)
{
ColorTypes enumValue = (ColorTypes) nm;
.......
你可以看看这篇文章的细节:http://www.jarloo.com/convert-an-int-or-string-to-an-enum/
您需要针对 select 列表的所有 option
值设置 Value
属性。
将以下下拉框用于 select 按值的文本表示形式:
@Html.DropDownListFor(model => model.Indicator, Enum.GetValues(typeof(ColorTypes)).Cast<ColorTypes>().Select(x => new SelectListItem { Text = x.ToString(), Value = x.ToString() }), new { @class = "form-control", id = "Indicator" })
或使用以下内容使其 select 达到 integer
值:
@Html.DropDownListFor(model => model.Indicator, Enum.GetValues(typeof(ColorTypes)).Cast<ColorTypes>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }), new { @class = "form-control", id = "Indicator" })
这将使您的 .Val()
jQuery 代码成为 select 正确的代码。