asp.net mvc 4 - How to pass data from Razor View Kendo UI DropDownList to Controller Variable? -
vs'12 , kendoui, asp.net c# mvc4 internet application ef code first
would see how 1 pass values form kendoui dropdownlist mvc controller razor view
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 , other variables null... wherein lies question why these 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() }; }
things have tried
- setting var text = dropdownlist.text();
- setting var ddltracts = $("#tracts").data("kendodropdownlist");
no matter try id wise or controller wise cannot values "read" in controller, nor can grab , pass values on in action links.
please help!!
updated code per comments mmillican below
string sct = model.sections; string trt = model.tracts;
viewmodel
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; } }
what have done far is:
- removed
[acceptverbs(httpverbs.post)]
,formcollection values
controller - removed
//.name("tracts")
, optional.htmlattributes(new { id = "tracts"})
each dropdownlist - added
dropdownlistfor(m=>m.tracts)
each ddl , imported@model og.modelview.viewmodelcctrst
customviewmodel can read below - renamed lowercase
.cascadefrom("clients")
(not clients) uppercase.cascadefrom("clients")
the tag below says alert("select tract upload:\n....); did alert 1 time during these changes, model , variable attempting send actionlink form razor view still both null , alert stopped popping up.
$(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 upload:\n" + clientsinfo + countysinfo + townshipsinfo + rangesinfo + sectionsinfo + tractsinfo); }); });
update
fixed syntax issue fixed scipt error. populating clientsinfo + countysinfo + townshipsinfo + rangesinfo + sectionsinfo + tractsinfo
- me controller?
you should use viewmodel
has properties in such as:
updated
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; } }
then kendo dropdownlist become dropdownlistfor<>
shown here:
@(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*** )
and can post controller in following way:
[httppost] public actionresult index(myviewmodel model) { if (modelstate.isvalid) { var newvar = model.clients;
when return viewmodel view (from controller) , myviewmodel.clients has value "client1", dropdownlist pick , have selected value.
hopefully you're looking / makes sense.
Comments
Post a Comment