Tim*_*sen 2 javascript c# asp.net-mvc asp.net-mvc-3
过去两天我试图弄清楚如何通过javascript帮助选择下拉列表中的新项目时动态更新复选框列表.
我一周前做过同样的事情,我应该从下拉列表更新到下拉列表.
到目前为止我得到了什么:
视图
@using (Html.BeginForm())
{
@Html.DropDownListFor(x => x.User, (IEnumerable<SelectListItem>)Model.UserList, "-- vælg bruger --")
if (Model.checkboxlist != null)
{
for (var i = 0; i < Model.checkboxlist.Count; i++)
{
<div class="editor-label">
@Html.CheckBoxFor(c => Model.checkboxlist[i].check)
@Html.LabelFor(c => Model.checkboxlist[i].Id, Model.checkboxlist[i].Id)
@Html.HiddenFor(c => Model.checkboxlist[i].Id)
</div>
}
}
}
<script type="text/javascript">
$('#User').change(function () {
alert('HEJ!');
var selectedUser = $(this).val();
alert(selectedUser);
if (selectedUser != null && selectedUser != '-- vælg bruger --' && selectedUser != '') {
$.getJSON('@Url.Action("getPdfCheckBoxList","Admin")', { username: selectedUser },
function (employee) {
var checkboxlist = $('#checkboxlist');
checkboxlist.empty();
$.each(employee, function (index, employee) {
checkboxlist.append($('<checkbox/>', {
checked = 'false'
}));
});
});
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
当我加载视图时Model.checkboxlist为null因为我不返回任何内容来模拟dropdownlist项目.
ControllerAction:
public ActionResult getPdfCheckBoxList(String username)
{
MethodService service = new MethodService();
var list = new List<PDFCheckBoxList>();
foreach (var pdfCheckBoxList in getPDFFileNames(username))
{
list.Add(new PDFCheckBoxList { check = false, Id = pdfCheckBoxList });
}
return Json(list, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
现在甚至警报("HEJ")甚至没有被解雇,我只是想弄清楚为什么......请帮助吗?
您可以使用部分视图并让控制器操作返回部分视图并在服务器上构建标记,而不是在客户端上执行此操作:
@using (Html.BeginForm())
{
@Html.DropDownListFor(
x => x.User,
Model.UserList,
"-- vælg bruger --",
data_url = Url.Action("getPdfCheckBoxList", "Admin")
)
<div id="checkboxlist">
@if (Model.checkboxlist != null)
{
@Html.Partial("_checkboxlist", Model.checkboxlist)
}
</div>
}
Run Code Online (Sandbox Code Playgroud)
然后定义_checkboxlist.cshtml:
@model IEnumerable<PDFCheckBoxList>
@{
ViewData.TemplateInfo.HtmlFieldPrefix = "checkboxlist";
}
@Html.EditorForModel()
Run Code Online (Sandbox Code Playgroud)
最后~/Views/Shared/EditorTemplates/PDFCheckBoxList.cshtml是为每个元素呈现的编辑器模板:
@model PDFCheckBoxList
<div class="editor-label">
@Html.CheckBoxFor(c => c.check)
@Html.LabelFor(c => c.Id, Model.Id)
@Html.HiddenFor(c => c.Id)
</div>
Run Code Online (Sandbox Code Playgroud)
现在你可以像这样修改你的javascript:
<script type="text/javascript">
// Now that we no longer have any server side
// code here we could externalize this script
// into a separate javascript file
$('#User').change(function () {
if (selectedUser != null && selectedUser != '') {
var selectedUser = $(this).val();
var url = $(this).data('url');
var data = { username: selectedUser };
$('#checkboxlist').load(url, data);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
最后你的getPdfCheckBoxList行动:
public ActionResult getPdfCheckBoxList(String username)
{
var service = new MethodService();
var list = getPDFFileNames(username)
.Select(x => new PDFCheckBoxList
{
check = false,
Id = x
})
.ToList();
return PartialView("_checkboxlist", list);
}
Run Code Online (Sandbox Code Playgroud)
如果你想在客户端上构建标记并让控制器操作返回JSON,我建议你使用一些javascript模板框架.
| 归档时间: |
|
| 查看次数: |
2336 次 |
| 最近记录: |