vs'12、KendoUI、asp.net C# MVC4 互联网应用程序 EF Code First
想了解如何将 KendoUI DropDownList 中的值从 Razor View 传递到 MVC Controller
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 和其他变量始终为 null...这就是我的问题为什么这些始终为 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()
};
}
我尝试过的事情
- 设置 var text = dropdownlist.text();
- 设置 var DDLtracts = $("#tracts").data("kendoDropDownList");
无论我尝试 ID 明智还是 Controller 明智,我都无法在 Controller 中获取要“读取”的值,也无法在操作链接中获取并传递值。
请帮忙!!
Updated Code Per Comments by mmillican help below
string sct = model.Sections;
string trt = model.Tracts;
View 模型
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; }
}
到目前为止我们所做的是:
- 从 Controller 中删除了
[AcceptVerbs(HttpVerbs.Post)]
和FormCollection 值
- 从每个 DropDownList 中删除了
//.Name("Tracts")
和可选.HtmlAttributes(new { id = "tracts"})
- 为每个 DDL 添加了
DropDownListFor(m=>m.Tracts)
并导入了@model OG.ModelView.ViewModelCCTRST
可以在下面读取 CustomViewModel - 将所有小写
.CascadeFrom("clients")
(不仅仅是客户端)重命名为大写.CascadeFrom("Clients")
下面的标签显示alert("Select Tract to Upload:\n....); 在这些更改期间实际上确实发出了 1 次警报,但是尝试通过 Razor View 的 Actionlink 发送的模型和变量是仍然为空并且警报停止弹出。
$(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+sectionInfo+tractsInfo
- 这是否可以帮助任何人帮助我将其发送到我的 Controller ?
最佳答案
您应该使用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***
)
然后您可以通过以下方式发布到 Controller :
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (ModelState.IsValid)
{
var newVar = model.Clients;
当您将 ViewModel 返回到 View (从 Controller )并且 MyViewModel.Clients 的值为“Client1”时,DropDownList 将选取该值并将其设为所选值。
希望这是您正在寻找的/有意义的。
关于asp.net-mvc-4 - 如何将数据从 Razor View Kendo UI DropDownList 传递到 Controller 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18410895/