如何从剃刀查看剑道UI DropDownList的数据传递给控制器​​的变量? [英] How to pass data from Razor View Kendo UI DropDownList to Controller Variable?

查看:227
本文介绍了如何从剃刀查看剑道UI DropDownList的数据传递给控制器​​的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vs'12,KendoUI,asp.net C#MVC4互联网应用EF code首先

想看看怎么一会传递值形成了从剃刀视图中KendoUI DropDownList的一个MVC控制器


  

控制器


  [HttpPost]
  //[AcceptVerbs(HttpVerbs.Post)
    公众的ActionResult指数(ViewModelCCTRST模型)//的FormCollection值)
    {        如果(ModelState.IsValid)
        {            字符串CLT = model.Clients;
            字符串CNT = model.Countys;
            字符串TWN = model.TownShips;
            ...
             ...
            //字符串XX =值[客户];
            // XX =值[Countys];
            // XX =值[乡];
                     ...
                      ...

CLT,CNT,TWN和其他变量总是空......其中,就在于我的问题为什么这些总是空的*



  

的Razor视图:


  @ @(Html.Kendo()DropDownListFor(M =方式> m.Ranges)
                      //.Name(\"ranges)
                      .HtmlAttributes(新{风格=宽度:300像素})// ID =范围})
                      .OptionLabel(选择范围...)
                      .DataTextField(RangeName)
                      .DataValueField(RangeID)
                      .DataSource(来源= GT; {
                          source.Read(读=>
                          {
                              read.Action(GetCascadeRanges,AddCCCTRST)
                                  。数据(filterRanges);
                          })
                          .ServerFiltering(真);
                      })
                      .Enable(假)
                      .AutoBind(假)
                      .CascadeFrom(城镇)
                )
                <脚本>
                    功能filterRanges(){
                        返回{
                            乡:$(#乡)VAL()。
                        };
                    }

的事情,我已经试过


  • 设置VAR文本= dropdownlist.text();

  • 设置VAR DDLtracts = $(#大片)的数据(kendoDropDownList);

不管什么我尝试ID明智或控制器明智的,我不能让值是读的控制器,我也不能抢,并通过在操作链接的值。

请帮助!



  

更新code的每按mmillican帮助评论如下


 字符串SCT = model.Sections;
                字符串TRT = model.Tracts;

视图模型

 公共类ViewModelCCTRST
{
    公共字符串的客户{搞定;组; }
    公共IEnumerable的<&dbclient中GT; AvailableClients {搞定;组; }
    公共字符串Countys {搞定;组; }
    公共IEnumerable的< dbCounty> AvailableCounties {搞定;组; }
    公共字符串乡{搞定;组; }
    公共IEnumerable的< dbTownShip> AvailableTownShip {搞定;组; }
    公共字符串范围{搞定;组; }
    公共IEnumerable的< dbRange> AvailableRanges {搞定;组; }
    公共字符串第{搞定;组; }
    公共IEnumerable的< dbSection> AvailableSection {搞定;组; }
    公共字符串大港{搞定;组; }
    公共IEnumerable的< dbTract> AvailableTracts {搞定;组; }
}

我们所做到目前为止:


  • 删除的[AcceptVerbs(HttpVerbs.Post)] 的FormCollection值从控制器

  • 删除 //。名称(大港)和可选的 .HtmlAttributes(新{ID =大片})从每个DropDownList的

  • 新增 DropDownListFor(M = GT; m.Tracts)每个DDL和进口 @model OG.ModelView.ViewModelCCTRST CustomViewModel可以阅读下面

  • 改名全部小写 .CascadeFrom(客户)(不只是客户端)为大写 .CascadeFrom(客户)

下面的地方说:警报标签(选择到道载:\\ n ....);这些变化过程中没有实际警报1次,但是模型和可变尝试用ActionLink的发送形成的Razor视图是仍然null和警报停止雨后春笋般冒出来。

  $(文件)。就绪(函数(){
        $(#GET)。点击(函数(){
            VAR的客户= $(#客户端)。数据(kendoDropDownList),
            countys = $(#Countys)。数据(kendoDropDownList),
            乡= $(#乡)。数据(kendoDropDownList),
            范围= $(#范围)。数据(kendoDropDownList),
            部分= $(#节)。数据(kendoDropDownList),
            大片= $(#大港)的数据(kendoDropDownList);      VAR clientsInfo =\\ nclients:{ID:+ clients.value()+,名称:+ clients.text()+},
      countysInfo =\\ ncountys:{ID:+ countys.value()+,名称:+ countys.text()+},
       ....
        ....      警报(选择道中上传:\\ n+ clientsInfo + countysInfo + townShipsInfo + rangesInfo + sectionsInfo + tractsInfo);
           });
    });

更新

固定的语法问题固定素文字错误。现在填充 clientsInfo + countysInfo + townShipsInfo + rangesInfo + sectionsInfo + tractsInfo - ?这是否帮助谁能帮助我得到这个我的控制器


解决方案

您应该使用视图模型有你的属性在里面,如:

更新

 公共类MyViewModel
{
    公共字符串的客户{搞定;组; }
    公共IEnumerable的<客户> AvailableClients {搞定;组; }
    公共字符串Countys {搞定;组; }
    公共IEnumerable的<客户> AvailableCounties {搞定;组; }
    公共字符串乡{搞定;组; }
    公共IEnumerable的<客户> AvailableTownShips {搞定;组; }
    公共字符串第{搞定;组; }
    公共IEnumerable的<客户> AvailableSection {搞定;组; }
    公共字符串大港{搞定;组; }
    公共IEnumerable的<&道GT; AvailableTracts {搞定;组; }
}

那么你的剑道的DropDownList将成为 DropDownListFor<> 如下所示:

  @(Html.Kendo()DropDownListFor(M =方式> m.Clients)
                  .HtmlAttributes(新{风格=宽度:300像素})// ID =客户})
                  .OptionLabel(选择客户端...)
                  .DataTextField(CLIENTNAME)
                  .DataValueField(客户端ID)
                  .DataSource(来源= GT; {
                       source.Read(读=> {
                           read.Action(GetCascadeClients,ImageUpload);
                       });
                  })
                 *** *** 1
            )

和则可以张贴到你的控制器通过以下方式:

  [HttpPost]
    公众的ActionResult指数(MyViewModel模型)
    {        如果(ModelState.IsValid)
        {            VAR newVar = model.Clients;

当您返回视图模型到视图(从Controller)和MyViewModel.Clients具有值客户端1,将DropDownList将挑选起来有它是选择的值。

希望这是你在找什么/是有道理的。

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

Would like to see how one would pass values form a KendoUI DropDownList to a MVC Controller from a Razor View

Controller

  [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 and other variables are always null... wherein lies my question why are these always null*


Razor View:

                @ @(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()
                        };
                    }

Things i have tried

  • setting var text = dropdownlist.text();
  • setting var DDLtracts = $("#tracts").data("kendoDropDownList");

No matter what i try id wise or controller wise I cannot get the values to be "Read" in the Controller, nor can i grab and pass the values on in action links.

Please help!!


Updated Code Per Comments by mmillican help below

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

What we have done so far is:

  • Removed [AcceptVerbs(HttpVerbs.Post)] and FormCollection values from controller
  • Removed //.Name("Tracts") and optional .HtmlAttributes(new { id = "tracts"}) from each DropDownList
  • Added DropDownListFor(m=>m.Tracts) for each DDL and imported @model OG.ModelView.ViewModelCCTRST CustomViewModel can be read below
  • Renamed all lowercase .CascadeFrom("clients") (not just clients) to uppercase .CascadeFrom("Clients")

The tag below where it says alert("Select Tract to Upload:\n....); did actually alert 1 time during these changes, however the Model and variable attempting to send with an Actionlink form the Razor View are still both null and the alert stopped popping up.

    $(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);
           });
    });

Update

fixed syntax issue fixed the scipt error. Is now populating clientsInfo + countysInfo + townShipsInfo + rangesInfo + sectionsInfo + tractsInfo - does this help anyone help me get this to my controller?

解决方案

You should use a ViewModel that has your properties in it such as:

Updated

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

Then your Kendo DropDownList would become a DropDownListFor<> as shown here:

@(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***
            )

And you can then post to your controller in the following way:

[HttpPost]
    public ActionResult Index(MyViewModel model)
    {

        if (ModelState.IsValid)
        {

            var newVar = model.Clients;

When you return the ViewModel to the View (from the Controller) and MyViewModel.Clients has the value "Client1", the DropDownList will pick that up and have it be the selected value.

Hopefully this is what you're looking for / makes sense.

这篇关于如何从剃刀查看剑道UI DropDownList的数据传递给控制器​​的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆