2013-08-23 53 views
2

vs'12,KendoUI,asp.net C#MVC4互聯網應用EF代碼第一次如何將Razor View Kendo UI DropDownList中的數據傳遞給控制器​​變量?

想看看怎麼一會傳遞值從Razor視圖

形成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和其他變量總是空...其中L IES我的問題爲什麼這些總是空**


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

的事情,我已經試過

  • 設置變種文字= dropdownlist.text ();
  • 設置var DDLtracts = $(「#tracts」)。data(「kendoDropDownList」);

無論我嘗試使用智能或控制器的智能我無法在控制器中獲取值「讀取」,我也無法在動作鏈接中獲取並傳遞值。

請幫忙!!下面

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


更新的代碼每評說mmillican幫助視圖模型

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的
  • 新增DropDownListFor(m=>m.Tracts)每個DDL //.Name("Tracts")和可選.HtmlAttributes(new { id = "tracts"})和進口@model OG.ModelView.ViewModelCCTRSTCustomViewModel可以在下面
  • 讀取更名全部小寫.CascadeFrom("clients")(不只是客戶)以大寫.CascadeFrom("Clients")

下面的標籤說警報(「Select Tract to Upload:\ n ....);在這些更改期間實際上已經提醒1次,但是試圖使用動態鏈接形式發送Razor視圖的模型和變量仍然爲空,並且警報停止彈出。

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

更新

固定的語法問題固定素文字錯誤。現在正在填充clientsInfo + countysInfo + townShipsInfo + rangesInfo + sectionsInfo + tractsInfo - 這是否有助於任何人幫助我將其獲取到我的控制器?

asdf

+1

請注意您的逗號和缺少分號。你正在創建一個名爲'alert'的變量 - 混淆了編譯器。這導致了各種各樣的錯誤。 – Brett

+0

@Brett感謝您的支持! –

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

那麼你的劍道的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; 

當您返回視圖模型到視圖(從Controller)和MyViewModel.Clients值爲「客戶端1」,將DropDownList會挑那就是它的選定價值。

希望這是你要找的/情理之中的事情。

+0

使得sence很快就會在這裏嘗試,客戶,countys,鄉鎮是全模型,但是,我不需要我的ViewModel中的客戶端集合 - 我對這個offtopic問題表示歉意,這將成爲我創建的第一個ViewModel。 –

+1

在這種情況下,是的。我會用我應該更新的東西來幫助你。如果您需要更多幫助,請告知我。 –

+1

我向ViewModel添加了一些屬性,我認爲這是您要求的。但是,現在我想到了,您正在使用AJAX綁定,因此您可能不需要這些綁定。如果你不想使用AJAX綁定,你可以在你的'DropDownListFor <>'中添加'.BindTo(Model.AvailableClients)',但是這對於級聯下拉菜單不起作用。 –

相關問題