我有一个MVC 5 APP&我正在使用@ Html.CheckBoxFor我加载了由其表格提供的1个Dropdown视图(Left Table = 10个项目)。在做出选择之后,我想对Controller Action执行Jquery GET并返回其表中可用的其他项的总列表(右表= 12项)。在连接表中查找由Id选择的项目(中间表=左和右连接)。我的ajax调用工作到GET到服务器,我的模型得到更新,当我运行Web检查器时,值存在,但我的视图没有更新。另外,我在最初的GET上硬编码Id我确实得到了12个复选框,据说从我的连接表中检查了8个按预期方式。我的问题是如何正确地将ajax返回到视图?谢谢你!
模型
namespace MPHComplianceTracker.Models
{
public class CheckBoxListModel
{
public int Id { get; set; } // Integer value of a checkbox
public string Name { get; set; } // String name of a checkbox
//public object Tags { get; set; } // Object of html tags to be applied to checkbox, e.g.: 'new { tagName = "tagValue" }'
public bool IsSelected { get; set; } // Boolean value to select a checkbox on the list
}
public class CheckkBoxListPostModel
{
public string[] SelectedIds { get; set; }
}
//Helper class to make posting back selected values easier
public class CheckBoxListViewModel
{
public IEnumerable<CheckBoxListModel> Selected { get; set; }
public IEnumerable<CheckBoxListModel> Available { get; set; }
public CheckkBoxListPostModel PostedItems { get; set; }
}
public class RelationshipsVM
{
public RelationshipsVM()
{
CheckboxList = new CheckBoxListViewModel();
}
public CheckBoxListViewModel CheckboxList { get; set; }
控制器
public ActionResult Relationships(int? id, CheckkBoxListPostModel PostedItems, RelationshipsVM relationshipsVm)
{
if (id == null)
{
ViewBag.ProtectedResourceKey = new SelectList
(db.ProtectedResources.Where(x => x.Active == true), "ListKey", "ListValue");
return View(relationshipsVm);
}
else
{
ViewBag.ProtectedResourceKey = new SelectList
(db.ProtectedResources.Where(x => x.Active == true), "ListKey", "ListValue");
relationshipsVm.CheckboxList.Available =
db.ComplianceRequirements.Where(x => x.Active == true)
.Select(x => new CheckBoxListModel
{
Id = x.ListKey,
Name = x.ListValue,
});
//id = 8;
List<int> complianceRequirementIds = db.MemberComplianceRequirements
.Where(x => x.ProtectedResourceID == id)
.Select(s => s.ComplianceRequirementID.Value).ToList();
relationshipsVm.CheckboxList.Selected =
db.ComplianceRequirements.Where(x => complianceRequirementIds.Contains(x.ListKey))
.Select(s => new CheckBoxListModel()
{
Id = s.ListKey,
Name = s.ListValue,
IsSelected = true
});
return View(relationshipsVm);
//return Json(new { success = true, }, JsonRequestBehavior.AllowGet);
}
}
的Ajax
$("#ProtectedResourceKey").on('change', function () {
var myData = $(this).val();
$.ajax({
type: 'GET',
url: '/Admin/Relationships',
dataType: "text",
contentType: "application/json; charset=utf-8",
data: { id: myData },
success: function (data) {
if (data) {
//I get the DATA HERE but do not know why it does not show up in the VIEW
alert("good");
}
else {
alert("bad");
}
}
});
});
HTML
<div class="col-md-4">
<label for="ProtectedResourceKey" class="control-label">Protected Resource:</label>
@Html.DropDownList("ProtectedResourceKey", null, "Select Protected Resource...", htmlAttributes:
new { @class = "form-control", @id = "ProtectedResourceKey" })
@Html.ValidationMessageFor(model => model.ProtectedResourceKey)
</div>
<div class="col-md-4" id="checkboxList">
<style type="text/css">
label {
display: inline-block !important;
padding: 10px !important;
}
</style>
@if ((Model.CheckboxList.Available) != null)
{
@Html.CheckBoxListFor(x => x.CheckboxList.PostedItems.SelectedIds,
x => x.CheckboxList.Available,
x => x.Id,
x => x.Name,
x => x.CheckboxList.Selected,
Position.Vertical_RightToLeft)
}
</div>