如何将数据从Razor View Kendo UI DropDownList传递给Controller变量?

时间:2013-08-23 19:58:31

标签: asp.net-mvc-4 razor kendo-ui kendo-asp.net-mvc model-view

vs'12,KendoUI,asp.net C#MVC4 Internet Application EF Code First

想看看如何从Razor View中将KendoUI DropDownList的值传递给MVC控制器

  

控制器

  [HttpPost]
  //[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(ViewModelCCTRST model) //, FormCollection  values)
    {

        if (ModelState.IsValid)
        {

            string clt = model.Clients;
            string cnt = model.Countys;
            string twn = model.TownShips;
            ...
             ...
            //string xx = values["Clients"];
            //       xx = values["Countys"];
            //       xx = values["TownShips"];
                     ...
                      ...

* clt,cnt,twn和其他变量总是为空的...其中有我的问题为什么这些变量总是为空**


  

Razor查看:

                @ @(Html.Kendo().DropDownListFor(m=>m.Ranges)
                      //.Name("ranges")
                      .HtmlAttributes(new { style = "width:300px"}) //, id = "ranges"})
                      .OptionLabel("Select Range...")
                      .DataTextField("RangeName")
                      .DataValueField("RangeID")
                      .DataSource(source => {
                          source.Read(read =>
                          {
                              read.Action("GetCascadeRanges", "AddCCCTRST")
                                  .Data("filterRanges");
                          })
                          .ServerFiltering(true);
                      })
                      .Enable(false)
                      .AutoBind(false)
                      .CascadeFrom("TownShips")
                )
                <script>
                    function filterRanges() {
                        return {
                            townShips: $("#TownShips").val()
                        };
                    }

我尝试过的事情

  • 设置var text = dropdownlist.text();
  • 设置var DDLtracts = $(“#tracts”)。data(“kendoDropDownList”);

无论我尝试id智能还是控制器,我都无法在Controller中获取值“Read”,也无法抓取并传递动作链接中的值。

请帮助!!


  

通过下面的mmillican帮助更新了代码每条评论

                string sct = model.Sections;
                string trt = model.Tracts;

Viewmodel

public class ViewModelCCTRST
{
    public string Clients { get; set; }
    public IEnumerable<dbClient> AvailableClients { get; set; }
    public string Countys { get; set; }
    public IEnumerable<dbCounty> AvailableCounties { get; set; }
    public string TownShips { get; set; }
    public IEnumerable<dbTownShip> AvailableTownShip { get; set; }
    public string Ranges { get; set; }
    public IEnumerable<dbRange> AvailableRanges { get; set; }
    public string Sections { get; set; }
    public IEnumerable<dbSection> AvailableSection { get; set; }
    public string Tracts { get; set; }
    public IEnumerable<dbTract> AvailableTracts { get; set; }
}

到目前为止我们所做的是:

  • 从控制器
  • 中删除了[AcceptVerbs(HttpVerbs.Post)]FormCollection values
  • 从每个DropDownList
  • 中删除了//.Name("Tracts")和可选的.HtmlAttributes(new { id = "tracts"})
  • 为每个DDL添加了DropDownListFor(m=>m.Tracts),导入的@model OG.ModelView.ViewModelCCTRST CustomViewModel可以在下面阅读
  • 将所有小写.CascadeFrom("clients")(不仅仅是客户)重命名为大写.CascadeFrom("Clients")

下面的标签显示警告(“选择要上传的路径:\ n ......);在这些更改期间确实提醒了1次,但是尝试使用Actionlink从Razor View发送的模型和变量是仍然都是null并且警报停止弹出。

    $(document).ready(function () {
        $("#get").click(function () {
            var clients = $("#Clients").data("kendoDropDownList"),
            countys = $("#Countys").data("kendoDropDownList"),
            TownShip = $("#TownShip").data("kendoDropDownList"),
            ranges = $("#Ranges").data("kendoDropDownList"),
            sections = $("#Sections").data("kendoDropDownList"),
            tracts = $("#Tracts").data("kendoDropDownList");

      var clientsInfo = "\nclients: { id: " + clients.value() + ", name: " + clients.text() + " }",
      countysInfo = "\ncountys: { id: " + countys.value() + ", name: " + countys.text() + " }",
       ....
        ....

      alert("Select Tract To Upload:\n" + clientsInfo + countysInfo + townShipsInfo + rangesInfo + sectionsInfo + tractsInfo);
           });
    });

更新

修复了语法问题修复了scipt错误。现在正在填充clientsInfo + countysInfo + townShipsInfo + rangesInfo + sectionsInfo + tractsInfo - 这有助于任何人帮助我将其传达给我的控制器吗?

asdf

1 个答案:

答案 0 :(得分:2)

您应该使用包含您属性的ViewModel,例如:

<强>更新

public class MyViewModel
{
    public string Clients { get; set; }
    public IEnumerable<Client> AvailableClients { get; set; }
    public string Countys { get; set; }
    public IEnumerable<Client> AvailableCounties { get; set; }
    public string TownShips { get; set; }
    public IEnumerable<Client> AvailableTownShips { get; set; }
    public string Sections { get; set; }
    public IEnumerable<Client> AvailableSection { get; set; }
    public string Tracts { get; set; }
    public IEnumerable<Tract> AvailableTracts { get; set; }
}

然后您的Kendo DropDownList将成为DropDownListFor<>,如下所示:

@(Html.Kendo().DropDownListFor(m => m.Clients)
                  .HtmlAttributes(new { style = "width:300px"}) //, id = "clients"})
                  .OptionLabel("Select Client...")
                  .DataTextField("ClientName")
                  .DataValueField("ClientID")
                  .DataSource(source => {
                       source.Read(read => {
                           read.Action("GetCascadeClients", "ImageUpload");
                       });
                  })
                 ***1***
            )

然后您可以通过以下方式发布到您的控制器:

[HttpPost]
    public ActionResult Index(MyViewModel model)
    {

        if (ModelState.IsValid)
        {

            var newVar = model.Clients;

当您将ViewModel返回到View(来自Controller)并且MyViewModel.Clients具有值&#34; Client1&#34;时,DropDownList将选择该值并使其成为所选值。

希望这是你正在寻找的/有意义的。