【发布时间】:2012-11-12 12:42:34
【问题描述】:
我的页面上有一个 ViewModel 和一个 DropDownList,其中包含一些值:
public class MyViewModel
{
public string SelectedItemDrop1 { get; set; }
public string SelectedItemDrop2 { get; set; }
public string SelectedItemDrop3 { get; set; }
public List<OptionViewModel> Options { get; set; }
}
public class OptionViewModel
{
public string Number { get; set; }
public string Option { get; set; }
}
而且,在我看来:
@using (Html.BeginForm("Save", "Controller", FormMethod.Post))
{
<ul id="cursos">
<li>
@Html.DropDownListFor(c => c.SelectedItemDrop1,
new SelectList(Model.Options, "Number", "Option", Model.SelectedItemDrop1))
Choose 1
</li>
<li>
@Html.DropDownListFor(c => c.SelectedItemDrop2,
new SelectList(Model.Options, "Number", "Option", Model.SelectedItemDrop2))
Choose 2
</li>
<li>
@Html.DropDownListFor(c => c.SelectedItemDrop3,
new SelectList(Model.Options, "Number", "Option", Model.SelectedItemDrop3))
Choose 3
</li>
</ul>
}
当我使用 Javascript 更改这些选择元素的选项时,我的返回值为 null。有什么问题?非常感谢!!!
编辑:
我的 javascript 代码:
$("#cursos li select").each(function (i, item) {
$(this).change(function () {
updateCursos($(this), 7);
});
$(this).blur(function () {
if ($(this).val() != -1) {
$(this).attr('disabled', 'disabled');
}
});
});
function updateCursos(select, avaiableCourses) {
var selecteds = '';
$("#cursos li select").each(function (i, item) {
var selected = $(item).val();
if (selected != -1)
selecteds += selected + ',';
});
var arr = selecteds.split(',');
$("#cursos li select").each(function (i, item) {
if ($(item).val() != select.val()) {
var oldValue = $(item).val();
if ($(item).val() == -1) {
var options = "<option value='-1'></option>";
for (i = 1; i <= avaiableCourses; ++i) {
if (select.val() != i && !contains(i, selecteds)) {
options += "<option value='" + i + "'>" + i + "ª option</option>";
}
}
options += "<option value='0'>Don't want it</option>";
$(item).children("option").remove();
$(item).html(options);
$(item).val(oldValue);
}
}
});
}
【问题讨论】:
-
你使用什么 JavaScript 代码?
-
当我将一个值更改为我的选择时,只需一个 jQuery 代码即可删除一些选项并插入另一个选项。如果我只是在更改触发器后禁用了该选项,我的 ViewModel 将变为 null。
-
如果问题仅在您使用此 jQuery 代码时出现,那么问题的原因很可能出在该代码中。
-
我们需要查看javascript。
-
好吧,我的选择选项基于我的 ViewModel 中的 IList。当我使用 jQuery 添加/删除选项时,我使用 ViewModel 生成的相同值。但它出现在 POST 上,我的 ViewModel 出现了一些错误。例如:我的 VM 生成:“',而我的 jQuery 代码生成相同的选项。那么,为什么会导致问题?
标签: c# asp.net-mvc-3 razor viewmodel