I have not been able to come across any documentation on how to "PASS" data from Razor View Drop Down Lists to My Controller or ActionLinks.
The Single DDL ( Drop Down List ) has been very successfull
- Razor
- @(Html.Kendo().DropDownList()
.OptionLabel("Select Role...")
.Name("DDLRoles")
.DataTextField("RoleName")
.DataValueField("RoleID")
.BindTo(ViewBag.DDLRoles)
.AutoBind(true))
- Controller [POST]
- [HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ChangeRole(UserProfiles userprofiles, FormCollection values)
{
string newRoleValue = values["DDLRoles"].Replace(",", "");
string something2 = userprofiles.UserName;
What i would like to know is how to do this with ( Passing into [Post Controller] action ) cascading DDL and Possibly how to use this value in an ActionLink.
Here is my code:
- Controller
- using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OG.ModelData;
using OG.Models;
using System.IO;
using Newtonsoft.Json;
namespace OG.Controllers
{
[Authorize(Roles = "PM,Administrator,CLS WebManager")]
public class AddCCCTRSTController : Controller
{
private OilNGasDB db = new OilNGasDB();
public ActionResult Index()
{
return View();
}
public JsonResult GetCascadeClients()
{
var clnts = db.Clients.Select(c => new { ClientID = c.ClientID, ClientName = c.Client }).ToList();
ViewBag.DDLclients = clnts;
return Json(db.Clients.Select(c => new { ClientID = c.ClientID, ClientName = c.Client }), JsonRequestBehavior.AllowGet);
}
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);
}
public JsonResult GetCascadeTownships(int countys)
{
var townships = db.TownShips.AsQueryable();
if (0 != countys)
{
townships = townships.Where(o => o.CountyID == countys);
}
return Json(townships.Select(o => new { TownshipID = o.TownshipID, TownshipName = o.Township }), JsonRequestBehavior.AllowGet);
}
[HttpPost] (THIS IS WHERE I'd LIKE TO RETURN DATA TOO FROM THE DDL's )
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection values)
{
if (ModelState.IsValid)
{
string more3 = values["clients"];
string more4 = values["text"];
string more5 = values["DDLtracts"];
string more6 = values["tracts"];
- // Return an empty string to signify success
return Content(more3 + more4 + more5 + more6);
}
else
{
return Content("");
}
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
} - RAZOR VIEW
<div class="demo-section">
<div>
<label for="clients">Client:</label>
@(Html.Kendo().DropDownList()
.Name("clients")
.HtmlAttributes(new { style = "width:300px"}) //, id = "clients"})
.OptionLabel("Select Client...")
.DataTextField("ClientName")
.DataValueField("ClientID")
.DataSource(source => {
source.Read(read => {
read.Action("GetCascadeClients", "AddCCCTRST");
});
})
.Events(e => e
.Select("dropdownlist_select")
.Change("dropdownlist_change")
)
)
</div>
<div>
<label for="countys">County:</label>
@(Html.Kendo().DropDownList()
.Name("countys")
.HtmlAttributes(new { style = "width:300px"}) //, id = "countys"})
.OptionLabel("Select County...")
.DataTextField("CountyName")
.DataValueField("CountyID")
.DataSource(source => {
source.Read(read =>
{
read.Action("GetCascadeCountys", "AddCCCTRST")
.Data("filterCountys");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
.CascadeFrom("clients")
)
<script>
function filterCountys() {
return {
clients: $("#clients").val()
};
}
</script>
</div>
<div>
<label for="townships">Township:</label>
@(Html.Kendo().DropDownList()
.Name("townships")
.HtmlAttributes(new { style = "width:300px"}) //, id = "townships"})
.OptionLabel("Select Township...")
.DataTextField("TownshipName")
.DataValueField("TownshipID")
.DataSource(source => {
source.Read(read =>
{
read.Action("GetCascadeTownships", "AddCCCTRST")
.Data("filterTownships");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
.CascadeFrom("countys")
)
<script>
function filterTownships() {
return {
countys: $("#countys").val()
};
}
</script>
</div>
<script>
$(document).ready(function () {
$("#get").click(function () {
alert("$(document).ready(function ()")
var clients = $("#clients").data("kendoDropDownList"),
countys = $("#countys").data("kendoDropDownList"),
townShips = $("#townShips").data("kendoDropDownList"),
alert("$(''#get'').click(function ()")
var clientsInfo = "\nclients: { id: " + clients.value() + ", name: " + clients.text() + " }",
countysInfo = "\ncountys: { id: " + countys.value() + ", name: " + countys.text() + " }",
townShipsInfo = "\ntownShips: { id: " + townShips.value() + ", name: " + townShips.text() + " }",
alert("Select Tract To Upload:\n" + clientsInfo + countysInfo + townShipsInfo);
});
});
</script>
@using (Html.BeginForm())
{
<p>- <input type="submit" value="ok" id="get" class="k-button"/>
</p>
}