2015-09-26 78 views
-1

我試圖按照下面的方法: 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" })> 

通過此獲得任何幫助錯誤將不勝感激! 謝謝!

回答

0

元素#genreDialog不存在於您的部分視圖中,因此您的代碼無法創建對話框。添加與idgenreDialog一個div你的局部視圖的結束,它應該工作 -

<div id="genreDialog"></div> 
0

的錯誤說,你要調用open方法上不存在的jQuery的對話框,讓你錯過了在您發佈的文章中提到的隱藏div

<div id="genreDialog" class="hidden"></div> 
相關問題