Friday, 23 August 2013

How to read KendoUI Cascading DropDownList from Controller

How to read KendoUI Cascading DropDownList from Controller

vs'12 , KendoUI, asp.net C# MVC4 Internet Application EF Code First
All of my Json Gets ( for the DDL's look like so )
public JsonResult GetCascadeCountys(int clients)
{
var Countys = db.Countys.AsQueryable();
if (0 != clients)
{
Countys = Countys.Where(p => p.ClientID == clients);
}
return Json(Countys.Select(p => new { CountyID = p.CountyID,
CountyName = p.County }), JsonRequestBehavior.AllowGet);
}
All my DDL in my view look like so
<label for="countys">County:</label>
@(Html.Kendo().DropDownList()
.Name("DDLcountys")
.HtmlAttributes(new { style = "width:300px", id = "countys"})
.OptionLabel("Select County...")
.DataTextField("CountyName")
.DataValueField("CountyID")
.DataSource(source => {
source.Read(read =>
{
read.Action("GetCascadeCountys", "ImageUpload")
.Data("filterCountys");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
.CascadeFrom("clients")
)
<script>
function filterCountys() {
return {
clients: $("#clients").val()
};
}
</script>
The script givin on the Tutorial Page of the KendoUI DropDownList
Documentation were these Altered for my DDL's
<script>
$(document).ready(function () {
var clients = $("#client").data("kendoDropDownList"),
countys = $("#county").data("kendoDropDownList"),
$("#get").click(function () {
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);
});
});
</script>
My Controller that i'm trying to read the User selected Data From
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> attachments,
string messageId, FormCollection values, )
{
if (ModelState.IsValid)
{
To read the Values I have tried the following
string something = values["DDLcountys"];
string something1 = values["countys"];
have tried reading from [DataSourceRequest] DataSourceRequest request



Please note that on my other view i have another KendoUI DDL - wich works
perfectly by grabbing the ID value. The difference between the 2 DDLs are
this
@(Html.Kendo().DropDownList()
.OptionLabel("Select Role...")
.Name("DDLRoles")
.DataTextField("RoleName")
.DataValueField("RoleID")
.BindTo(ViewBag.DDLRoles)
.AutoBind(true))
I'm using ViewBag to Bind to it instead of Method Linq Chains
I'm simply setting the name attribute instead of both
1. .Name("DDLtracts")
2. .HtmlAttributes(new { style = "width:300px", id = "tracts"})
My Question is how to read a KendoUI "Cascading" DDL Correctly from my
[HTTPPOST] Controller

No comments:

Post a Comment