【问题标题】:MVC Kendo Combobox selected item value?MVC Kendo Combobox选中的项目值?
【发布时间】:2014-11-18 07:46:17
【问题描述】:

在我看来,我正在使用 Kendo Combo Box。我将此组合框绑定到模型中的 Enum 类型。但是当我运行应用程序并更改下拉列表中的值时。始终只选择第一个值并将其传递给模型。

//MyModel
public class Mymodel
{
   public IsPerson IsPerson { get; set; }
}

public enum IsPerson 
    {
          yes,
          No
    }
//index.cshtml

 @(Html.Kendo().ComboBoxFor(x => x.MyModel.IsPerson )
        .Name("IsPerson ")
        .Placeholder("Select IsPerson ..")
                .BindTo(Enum.GetNames(typeof(App2_MVC.Models.IsPerson))))

是什么原因?即使我在下拉列表中选择“否”,我也会将所选项目设为“0”,所以值“是”??

【问题讨论】:

标签: asp.net-mvc kendo-ui asp.net-mvc-5


【解决方案1】:

不是将组合框绑定到枚举,而是将其绑定到键、值对的对象。还要定义“DataTextField 和 DataValueField”以获得所选元素的正确值。

@(Html.Kendo().ComboBoxFor(model => model.IsPerson)
 .Placeholder("----- Select -----")
 .DataTextField("Value")
 .DataValueField("Key")
 .DataSource(source => source.Read(read => read.Action("MyActionMethod", "Controller")))
)

Action 方法应该返回 IEnumerable 对象,其中“Model”是“Key and value”对。即

public class Model
{
    public string Value{ get; set; }

    public int Key{ get; set; }
}

在控制器中:

public ActionResult MyActionMethod()
    {
        var model= new Model 
                {
                    Value = "xyz",
                    Key = 1
                };
                var selectList = new List<Model>();
                selectList.Add(model);
                // Also can add multiple objects
                // Or add your database code to fetch the values from db table

        return Json(result, JsonRequestBehavior.AllowGet);
    }

根据您的代码:

     @model myapplication.Models.MyDetails

       <div>
    @using (Html.BeginForm("PostMymethod", "Home", FormMethod.Post))
    {
        @(Html.Kendo().PanelBar()
    .Name("Intro")
    .ExpandAll(false)
    .ExpandMode(PanelBarExpandMode.Single)
    .Items(items =>
    {
        items.Add()
            .Text("Jquery")
            .Content(@<text>

                @(Html.Kendo().DropDownListFor(x => x.sismember)
                            .DataTextField("value")
                    .DataValueField("key")
                    .DataSource(source =>
                    {
                        source.Read(read =>
                        {
                            read.Action("MyActionMethod", "Home");
                        });
                    })
                    )

            </text>);

    })
        )
        <table>
            <tr>
                <td class="savebtns">
                    <input type="submit" name="Command" value="Save" id="savebtn" class="=btn" />
                </td>
            </tr>
            </table>
    }
</div>

模型属性:

public class MyDetails
{
 public string sismember { get; set; }//this is the property im binding to combobox
}

其中 Ismember 是一个具有键和值的类:

public class IsMember
    {
        public string value { get; set; }

        public int key { get; set; }
    }

我想在这个控制器中获得选择的组合框值:MyController

 public class HomeController : Controller
    {
 [HttpPost]
        public ActionResult PostMymethod(MyDetails myDataModel,string command)
        {
            if (command == "Save")
            {
                return View("Home", myDataModel);
            }

           return View("Home", myDataModel);
        }
}

【讨论】:

  • 如何获取这个组合框的选中值??
  • 使用:var selectedValue = $('#IsPerson').data('kendoComboBox').value();注意:“IsPerson”是定义组合框的模型属性。此外,如果此答案解决了您的问题,请接受答案并关闭问题,如有其他疑问,请提出新问题。很高兴为您提供帮助。:)
  • 我想在“IsModel”属性中获得选定的组合框值。但我总是只得到 NULL。只有选定的值不会以任何方式绑定到模型..需要你的帮助@Satya Ranjan Sahoo
  • 还有一件事。是否有必要编写脚本函数来获取选定的值我想在没有任何脚本函数的情况下获取选定的值。
  • 优秀的方法!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
相关资源
最近更新 更多