2

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

想看看如何将 KendoUI DropDownList 中的值从 Razor 视图传递到 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 和其他变量始终为空......我的问题是为什么这些始终为空**


剃刀视图:

                @ @(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 中将值“读取”,也无法在操作链接中抓取并传递这些值。

请帮忙!!


由 mmillican 帮助在下方更新了每条评论的代码

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

视图模型

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"})
  • DropDownListFor(m=>m.Tracts)为每个 DDL添加和导入@model OG.ModelView.ViewModelCCTRST 的 CustomViewModel 可以在下面阅读
  • 将所有小写.CascadeFrom("clients")(不仅仅是客户端)重命名为大写.CascadeFrom("Clients")

下面的标签显示 alert("Select Tract to Upload:\n....); 在这些更改期间实际上确实警告了 1 次,但是尝试使用 Actionlink 从 Razor 视图发送的模型和变量仍然都是 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- 这是否有助于任何人帮助我将其发送到我的控制器?

自卫队

4

1 回答 1

2

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

更新

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 返回到视图(从控制器)并且 MyViewModel.Clients 具有值“Client1”时,DropDownList 将选择该值并将其作为选定值。

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

于 2013-08-26T22:24:27.697 回答