我的jquery就像这样
<script type="text/javascript">$(document).ready(function(){
$("#drpId").change(function(e) {
firstDDLValue = $("#drpId").val();
$.post('@Url.Action("GetDataForSecond", "Login")', { fstValue: firstDDLValue }, function (e) {
var select = $("#secondDDLID");
select.empty();
$.each(result, function (index, Data) {
select.append($('<option/>', {
value: result.Value,
}));
});
});
});
});
我的控制器代码是
public JsonResult GetDataForSecond(string fstValue)
{
Domain obj = new Domain();
var data = obj.LoadSubDomain(fstValue); return Json(data, JsonRequestBehavior.AllowGet);
在数据中我获得了第二次下拉列表的所有值但是在视图中它没有约束力。 我的视图代码就像这样
<div class="editor-field">
@{
var data = ViewBag.MyData;
<select name="drpId" id="drpId">
<option value="yourvalue">Select DomainName </option>
@foreach (var te in data)
{
<option>@te.DomainName</option>
}
</select>
}
@Html.ValidationMessageFor(model => model.DomainName)
</div>
<div class="editor-field">
<select name="secondDDLID" id="secondDDLID">
<option value="yourvalue"></option>
</select>
</div>
答案 0 :(得分:1)
这样做:
在您的视图中添加此脚本:
$("#drpId").change(function () {
firstDDLValue = $("#drpId").val();
$.post('@Url.Action("GetDataForSecond", "Login")', { fstValue: firstDDLValue }, function (result) {
var select = $("#secondDDLID");
select.empty();
$.each(result, function (index, Data) {
select.append($('<option/>', {
value: Data.Value,
text: Data.Text
}));
});
});
});
在控制器中绑定第二个ddl的方法:
public JsonResult GetDataForSecond(string fstValue)
{
Domain obj = new Domain();
var result = obj.LoadSubDomain(fstValue);
IList < SelectListItem > Data = new List<SelectListItem>();
for (int i = 0; i < result.Count; i++)
{
Data.Add(new SelectListItem()
{
Text = result[i].Name,
Value = result[i].Name,
});
}
return Json(Data, JsonRequestBehavior.AllowGet);
}