Asp.net mvc 4 如何将数据从Razor View Kendo UI DropDownList传递到控制器变量?
vs'12,KendoUI,asp.net C#MVC4互联网应用程序EF代码优先 希望了解如何从Razor视图将KendoUI下拉列表中的值传递给MVC控制器 控制器 *clt、cnt、twn和其他变量始终为空。。。我的问题是为什么这些总是空的**Asp.net mvc 4 如何将数据从Razor View Kendo UI DropDownList传递到控制器变量?,asp.net-mvc-4,razor,kendo-ui,kendo-asp.net-mvc,model-view,Asp.net Mvc 4,Razor,Kendo Ui,Kendo Asp.net Mvc,Model View,vs'12,KendoUI,asp.net C#MVC4互联网应用程序EF代码优先 希望了解如何从Razor视图将KendoUI下拉列表中的值传递给MVC控制器 控制器 *clt、cnt、twn和其他变量始终为空。。。我的问题是为什么这些总是空的** Razor视图: 视图模型 public class ViewModelCCTRST { public string Clients { get; set; } public IEnumerable<dbClient>
Razor视图: 视图模型
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; }
}
更新
修复了语法问题,修复了scipt错误。正在填充clientsInfo+countysInfo+townshipssinfo+rangesInfo+sectionsInfo+tractsInfo
-这有助于任何人将此信息发送到控制器吗
您应该使用包含您的属性的
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; }
}
然后,您可以通过以下方式发布到控制器:
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (ModelState.IsValid)
{
var newVar = model.Clients;
当您将ViewModel返回到视图(从控制器)和MyViewModel.Clients具有值“Client1”时,DropDownList将拾取该值并将其作为所选值
希望这就是你想要的/有意义的。注意逗号和分号的缺乏。您正在创建一个名为“alert”的变量,这会使编译器感到困惑。这会导致各种各样的错误。@Brett谢谢你抓住了它!Bump,还有其他想法吗?要将数据发送到控制器,一个简单的方法是将其字符串化并通过ajax POST发送。你没有粘贴HTML标记,所以我不知道你现在是怎么做的——无论是通过表单提交还是通过ajax<代码>$.ajax({url:'http://localhost//,数据:JSON.stringify({clients:{…},countys:{…},etc.}),类型:'POST',内容类型:'application/JSON'})很有意义,不久将在这里试用,客户机、国家、乡镇都是完整的模型,但是,我不需要在我的ViewModel中收集客户机的Icollection-我对主题外的问题表示歉意,这将是我创建的第一个ViewModel。在这种情况下,是的。我会用一些对你有帮助的东西更新我的答案。如果您需要更多帮助,请告诉我。我向ViewModel添加了一些属性,我认为这些属性正是您需要的。然而,现在我想起来了,您使用的是AJAX绑定,所以您可能不需要这些绑定。如果您不想使用AJAX绑定,可以在
DropDownListFor
中添加.BindTo(Model.AvailableClients)
,但这对级联下拉列表不起作用。请尝试删除DropDownlistStrange上的.Name(“节”)
。尝试从POST Action方法中删除FormCollection值
param。另外,[AcceptVerbs(HttpVerbs.Post)]
是多余的,可以删除。
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; }
}
$(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);
});
});
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; }
}
@(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;