【问题标题】:Selecting Enum items on AJAX call在 AJAX 调用中选择枚举项
【发布时间】:2015-04-16 07:39:19
【问题描述】:

我正在处理返回 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>

除了枚举类型的“指标”字段之外,我正在正确获取数据并正确加载。

如何从我得到的数据中选择一个枚举列表项。

例如:

0,1,2,3 索引顺序

【问题讨论】:

  • 您的 ajax 在 SalesController 上调用 LoadCustomerInfo() 并传递一个名为 custm 的参数,但您显示的方法是 loadInf(string nm)。您似乎没有包含正确的方法。
  • 是的,它确实有匹配的顺序是什么意思? vdata[0].Indicator 输出什么?它是否与您的选项之一的 value 属性完全匹配?
  • "What does vdata[0].Indicator output"有什么不明白的地方?在您的代码中放入console.log( vdata[0].Indicator); 并检查该值是什么并将其与为select 中的选项生成的html 进行比较)并阅读我的第二条评论-您显示了错误的代码(您调用了一个名为@987654332 的方法@不是loadInf())
  • 我同意我们确实需要知道 vdata[0].Indicator 是枚举的字符串表示还是整数值。无论哪种方式,请查看我发布的新答案。您的 jquery 未选择正确的值,因为您的选项上的 value="" 属性未填充。

标签: ajax asp.net-mvc enums


【解决方案1】:

您需要针对选择列表的所有option 值设置Value 属性。

为您的下拉框使用以下内容以按值的文本表示进行选择:

@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" })

或者使用下面的代码来选择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 代码选择正确的代码。

【讨论】:

  • 感谢您的回复,+1 您的时间和帮助,我会检查并接受答案
  • 没问题。让我知道它是怎么回事,我刚刚测试过它,它工作正常:)
  • @Coulton。我认为您误解了 OP 代码。 ajax 调用从另一个控件(可能是另一个下拉列表)传递一个值,以返回 SalesVM 类型的对象,并使用 SalesVM 的属性更新视图中的其他控件,包括带有 &lt;select id="Indicator" ..&gt; OP 的下拉列表声称其他控制值已正确更新,但 Indicator 未正确更新,这意味着返回的 Indicator 的值与选项之一的值不匹配。
  • 我明白你在说什么...... 1 秒
  • 请查看更新,它应该根据vdata[0].Indicator 的值起作用。如果它是 text 表示,那么它应该可以工作。如果是数字表示,请告诉我
【解决方案2】:

如果您检索字符串变量 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2018-12-14
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    相关资源
    最近更新 更多