我試圖按照下面的方法: http://www.asp.net/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/adding-a-new-category-to-the-dropdownlist-using-jquery-ui 要添加一個模式對話框來添加一個新的值與模態對話框的組合框。但是,我不斷收到以下錯誤:ASPMVC試圖顯示對話框將新項目添加到Combobox
0x800a139e - JavaScript運行時錯誤:無法在初始化之前調用對話框上的方法;試圖調用方法 '開放'
我的代碼如下:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function() {
$("#genreDialog").dialog({
autoOpen: false,
width: 400,
height: 300,
modal: true,
title: 'Add Genre',
buttons: {
'Save': function() {
var createGenreForm = $('#createGenreForm');
if (createGenreForm.valid()) {
$.post(createGenreForm.attr('action'), createGenreForm.serialize(), function (data) {
if (data.Error != '') {
alert(data.Error);
}
else {
// Add the new genre to the dropdown list and select it
$('#GenreId').append(
$('<option></option>')
.val(data.Genre.GenreId)
.html(data.Genre.Name)
.prop('selected', true) // Selects the new Genre in the DropDown LB
);
$('#genreDialog').dialog('close');
}
});
}
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
$('#genreAddLink').click(function() {
var createFormUrl = $(this).attr('href');
$('#genreDialog').html('')
.load(createFormUrl, function() {
// The createGenreForm is loaded on the fly using jQuery load.
// In order to have client validation working it is necessary to tell the
// jQuery.validator to parse the newly added content
jQuery.validator.unobtrusive.parse('#createGenreForm');
$('#genreDialog').dialog('open');
});
return false;
});
});
</script>
}
筆者認爲:
@Html.Partial("_ChooseCityDistrict")
我PartialView:
@Html.DropDownListFor(model => model.CityDistrictID, Model.CityDistrictList, "Select Name City District", htmlAttributes: new { @class = "form-control" })
<a class="button" href="@Url.Content("~/NameCityDistricts/Create")" id="genreAddLink">Add New City District</a>
@Html.ValidationMessageFor(model => model.CityDistrictID, "", new { @class = "text-danger" })>
通過此獲得任何幫助錯誤將不勝感激! 謝謝!