MVC Kendo Combobox选择了项目值?

时间:2014-11-18 07:46:18

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

我在视图中使用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'如此价值'是' ??

1 个答案:

答案 0 :(得分:4)

而不是将组合框绑定到枚举,而不是将其绑定到键,值对的对象。还要定义“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<型号>对象,其中“模型”是“键和值”对。即。

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; }
    }

我想在此控制器中获取选择的combox值:MyController

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

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