2015-09-26 46 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> 
相关问题