我有一个加载的局部视图主网页,其中使用Asp.net MVC更新下拉列表提交的模式弹出
Html.BeginCollectionItem(“雇员”)
因为我正在使用jQuery选项卡。 (所以用户可以添加更多的员工表格)
我的员工内部部分视图有一个选择国家的下拉列表,并且它还有一个链接弹出一个模式来添加一个新的国家。在这里,我想要做的是,在提交模式并关闭模式后,我希望立即刷新下拉列表,以便用户可以看到新的更新列表。
当用户添加更多选项卡时,dropdownlist的ID是否动态更改时,如何完成此操作?
Company.cshtml:
@using (Html.BeginForm("Create", "CompanyForm", FormMethod.Post))
{
<p>Company Info</p>
@Html.EditorFor(m => m.companyName, new { htmlAttributes = new { @class = "form-control" } })
<div id="tabs">
<ul>
<li><a href="#tabs-employee">Employee 1</a> <span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>
</ul>
<div id="tabs-employee">
@Html.Action("Employee")
</div>
</div>
<button type="submit">Submit</button>
}
<script>
//tab codes...
</script>
Employee.cshtml:
<div class="editorRow">
@using (Html.BeginCollectionItem("employees"))
{
<!-- Dropdown-->
@Html.DropDownListFor(m => m.CountryId, (SelectList)ViewBag.CountryList, "-- Select --", new { @class = "form-control" })
<!-- Link-->
<a href="#" class="btn btn-info btn-md" data-toggle="modal" data-target="#countryModal">Add Country</a>
}
</div>
<!-- Modal -->
<div class="modal fade" id="countryModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Country</h4>
</div>
<div class="modal-body">
<div class="ajax-dynamic-get-data-form" />
</div>
</div>
</div>
</div>
<script>
$('#countryModal').on('show.bs.modal', function (event) {
var url='@Url.Action("NewEmployee")';
$.ajax({
type: "GET",
url: url,
data: JSON,
cache: false,
success: function (data) {
$('#countryModal').find('.ajax-dynamic-get-data-form').html(data);
},
error: function (err) {
console.log(err);
}
});
})
</script>
NewCountry.cshtml:
@{
Layout = null;
}
@using (Html.BeginForm("PostNewCountry", "CompanyForm", FormMethod.Post, new { id = "postOfferForm" }))
{
<label>Employee Name</label>
@Html.TextBoxFor(x => x.CountryName, new { @class = "form-control" })
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-default">Save</button>
</div>
}
<script>
$("#postOfferForm").submit(function() {
$.ajax({
url: "/CompanyForm/PostNewCountry",
type: "POST",
data: $(this).serialize(),
datatype: "json",
success: function (response) {
if (response.status === "success") {
$('#countryModal').modal('hide');
} else {
alert(response.errorMessage);
$('#countryModal').modal('hide');
}
}
});
return false;
});
</script>
编辑
Stephen给了我一个提示来解决上述问题的另一个问题是,模式只会在tab1中弹出。我怎样才能更新所有选项卡中的下拉列表?
想要将新员工添加到所有下拉列表中,还是只添加“当前”选项卡上的员工?如果以后使用类名和相对选择符('id'属性的值无关紧要) –
太好了。使用类名作品,但我只是发现模式只弹出在雇员1选项卡。如果我点击员工2选项卡中的新员工链接,模式显示在员工1选项卡中。我认为它与这条线有关? $('#employeeModal')。find('.ajax-dynamic-get-data-form')。html(data); – pavilion
我很难理解你想在这里做什么。看起来你为'Company'生成了一个表单,并希望能够动态地添加新的'Employee'对象(并编辑它可能拥有的任何现有员工) - 在这种情况下,这没有任何意义 - 为什么你需要一个下拉列表员工? –