我有一個加載的局部視圖主網頁,其中使用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'對象(並編輯它可能擁有的任何現有員工) - 在這種情況下,這沒有任何意義 - 爲什麼你需要一個下拉列表員工? –