问题描述
||
如何将Telerik MVC组合框与美国状态列表绑定并将值从View传递到控制器?
将Telerik MVC ComboBox与美国状态列表绑定,然后将Selected值传递回Controller。
我想使用Javascript函数绑定Telerik MVC ComboBox。我已在Javascript函数中创建了美国状态列表,并将此美国状态列表分配给了我的ComboBox ...要将选定的美国状态发送回Controller,我已经准备好了HTML.TextBoxFor并将其可见性设置为hidden ...我的控制器我试图访问此垂直TextBox的值,但始终显示为null .. Plz帮助
以下是我的代码:-
<%= Html.Telerik().ComboBox()
.Name(\"ComboBox\")
.ClientEvents(events => events
.OnDataBinding(\"onDataBinding\")
.OnChange(\"getComboBox\")
)
%>
<%: Html.TextBoxFor(model => model.EnrolRegAcntDescriptor.state,new { ID = \"statetext\",style = \"visibility:hidden;width:10px\" })%>
<script type=\"text/javascript\">
function getComboBox() {
//\"ComboBox\" is the value specified by the Name() method.
debugger;
var comboBox = document.getElementById(\'ComboBox-value\').value;
var combotext = document.getElementById(\'ComboBox-input\').value; //$(\"#ComboBox\").data(\"tComboBox\");
var index = $(\"#ComboBox\").data(\"tComboBox\").value();
// var text = combotext.options[combotext.selectedindex].value;
// var MySelectedvalue = document.getElementByid(\'tComboBox\').options[document.getElementByid(\'tComboBox\').selectedindex].value
var states = document.getElementById(\'statetext\').value;
states = combotext;
// comboBox.select(index);
//alert(index);
return states;
}
<script type=\"text/javascript\">
function onDataBinding(e) {
var comboBox = $(\'#ComboBox\').data(\'tComboBox\');
comboBox.dataBind([
{ Text: \"ALABAMA\",Value: \"ALABAMA\" },{ Text: \"ALASKA\",Value: \"ALASKA\",Selected: true },{ Text: \"ARIZONA \",Value: \"ARIZONA\" },{ Text: \"ARKANSAS\",Value: \"ARKANSAS\" },{ Text: \"CALIFORNIA\",Value: \"CALIFORNIA\" },{ Text: \"COLORADO\",Value: \"COLORADO\" },{ Text: \"CONNECTICUT\",Value: \"CONNECTICUT\" },{ Text: \"DELAWARE \",Value: \"DELAWARE\" },{ Text: \"FLORIDA\",Value: \"FLORIDA\" },{ Text: \"GEORGIA\",Value: \"GEORGIA\" },{ Text: \"HAWAII\",Value: \"HAWAII\" },{ Text: \"IDAHO\",Value: \"IDAHO\" },{ Text: \"ILLINOIS\",Value: \"ILLINOIS\" },{ Text: \"INDIANA\",Value: \"INDIANA\" },{ Text: \"IOWA\",Value: \"IOWA\" },{ Text: \"KANSAS\",Value: \"KANSAS\" },{ Text: \"KENTUCKY\",Value: \"KENTUCKY\" },{ Text: \"LOUISIANA\",Value: \"LOUISIANA\" },{ Text: \"MAINE\",Value: \"MAINE\" },{ Text: \"MARYLAND\",Value: \"MARYLAND\" },{ Text: \"MASSACHUSETTS\",Value: \"MASSACHUSETTS\" },{ Text: \"MICHIGAN\",Value: \"MICHIGAN\" },{ Text: \"MINnesOTA\",Value: \"MINnesOTA\" },{ Text: \"MISSISSIPPI\",Value: \"MISSISSIPPI\" },{ Text: \"MISSOURI\",Value: \"MISSOURI\" },{ Text: \"MONTANA\",Value: \"MONTANA\" },{ Text: \"NEBRASKA\",Value: \"NEBRASKA\" },{ Text: \"NEVADA\",Value: \"NEVADA\" },{ Text: \"NEW HAMPSHIRE\",Value: \"NEW HAMPSHIRE\" },{ Text: \"NEW JERSEY\",Value: \"NEW JERSEY\" },{ Text: \"NEW MEXICO\",Value: \"NEW MEXICO\" },{ Text: \"NEW YORK\",Value: \"NEW YORK\" },{ Text: \"norTH CAROLINA\",Value: \"norTH CAROLINA\" },{ Text: \"norTH DAKOTA\",Value: \"norTH DAKOTA\" },{ Text: \"OHIO\",Value: \"OHIO\" },{ Text: \"OKLAHOMA\",Value: \"OKLAHOMA\" },{ Text: \"OREGON\",Value: \"OREGON\" },{ Text: \"PALAU\",Value: \"PALAU\" },{ Text: \"PENNSYLVANIA\",Value: \"PENNSYLVANIA\" },{ Text: \"RHODE ISLAND\",Value: \"RHODE ISLAND\" },{ Text: \"SOUTH CAROLINA\",Value: \"SOUTH CAROLINA\" },{ Text: \"SOUTH DAKOTA\",Value: \"SOUTH DAKOTA\" },{ Text: \"TENnesSEE\",Value: \"TENnesSEE\" },{ Text: \"TEXAS\",Value: \"TEXAS\" },{ Text: \"UTAH\",Value: \"UTAH\" },{ Text: \"VERMONT\",Value: \"VERMONT\" },{ Text: \"VERGINIA\",Value: \"VERGINIA\" },{ Text: \"WASHINGTON\",Value: \"WASHINGTON\" },{ Text: \"WEST VIRGINIA\",Value: \"WEST VIRGINIA\" },{ Text: \"WISCONSIN\",Value: \"WISCONSIN\" },{ Text: \"WYOMING\",Value: \"WYOMING\" }
],true /*preserve state*/);
}
解决方法
我自己找到了答案。代码中做了如下几处更改。
<%: Html.TextBoxFor(model => model.EnrolRegAcntDescriptor.state,new { type=\"Hidden\",ID = \"statetext\",style = \"width:10px\" })%>
function getComboBox() {
var combobox = document.getElementById(\'ComboBox-value\').value;
var combotext = document.getElementById(\'ComboBox-input\').value;
var index = $(\"#ComboBox\").data(\"tComboBox\").value();
var states = document.getElementById(\'statetext\').value;
document.getElementById(\'statetext\').value = index;
// alert(index);
return states;
}