【发布时间】:2014-04-16 08:49:48
【问题描述】:
查看
@Html.DropDownListFor(model => model.PracticGropupId, new SelectList(Model.PracticeGroup, "Value", "Text"))
@Html.DropDownListFor(model => model.SubPracticeGroupId, Enumerable.Empty<SelectListItem>(), new { style = "width:250px;" })
JS
$('#PracticGropupId').change(function () {
var selectedGroupId = $(this).val();
$.getJSON('/Business/SubPracticeGroup', { practiceGroup: selectedGroupId }, function (subGroups) {
var subGroupsSelect = $('#SubPracticeGroupId');
subGroupsSelect.empty();
subGroupsSelect.append(
$('<option/>')
.attr('value', '')
.text('Subgroup')
);
$.each(subGroups, function (index, subGroup) {
subGroupsSelect.append(
$('<option/>')
.attr('value', subGroup.Code)
.text(subGroup.Name)
);
});
});
});
如果服务器端验证失败,则提交时出现错误消息,并且 SubPracticeGroupId 下拉列表中的值变为空白。这也发生在我使用 jQuery ajax 动态生成的 HTML 上。有没有办法在服务器端验证后保留这些值?
【问题讨论】:
-
这是因为您将绑定的选定值发送回
ActionResult而不是列表,因为列表没有绑定到任何东西。 -
你应该使用 Ajax.BeginForm 而不是 Html.BeginForm
-
@ehsan Sajjad 使用 Ajax.BeginForm 会保留该值,但会丢失所有错误消息。我应该怎么做才能显示错误消息。
-
你在谈论验证消息??
-
是的,我说的是验证消息。
标签: jquery validation asp.net-mvc-4