2013-08-09 4 views
0

내 사업부는 내가 기대하고있는 무슨 내용을 표시되지 작동하지innerHTML을 상당

내 controllerAction :

/// <summary> 
    /// GetCountiresForManufacturer 
    /// </summary> 
    /// <returns></returns> 
    [Authorize(Roles = "Administrator")] 
    [AcceptVerbs(HttpVerbs.Get)] 
    public string GetCountiresForManufacturer(string manufacturer) 
    { 
     var sb = new StringBuilder(); 
     var rows = new List<Manufacturer>(); 
     var data = new Dictionary<string, List<Country>>(); 
     // var countries = new List<Country>(); 
     if (manufacturer.StartsWith(",")) 
     { 
      manufacturer = manufacturer.TrimStart(','); 
     } 
     string[] manufacturers = manufacturer.Split(','); 
     foreach (var s in manufacturers) 
     { 
      var manufacturerRow = _manufacturerRepository.GetManufacturerByName(s); 
      rows.Add(manufacturerRow); 
     } 

     foreach (var row in rows) 
     { 
      if(row==null) 
      { 
       continue; 
      } 
      var countriesList = _countryRepository 
      .GetAllCountriesForManufacturer(row.Id); 
      // countries.Add(countriesList); 
      data.Add(row.Description, countriesList.ToList()); 
     } 
     foreach (var s in data) 
     { 
      sb.Append(" <input id =\"Brand\" name=\"Brand\" value=\""+s.Key+"\"/>"); 
      foreach (var manufacturer1 in s.Value) 
      { 
       sb.Append("<input id =\"Dove\" type=\"checkbox\" 
       value=\""+manufacturer1.Name+"\"/>"); 
      } 
     } 


     return sb.ToString(); 
    } 

내보기 :

 <%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<EmsAdmin.Models.User>" %> 
    <%@ Import Namespace="EmsAdmin.Models" %> 

    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and 
     try again.") %> 
    <% using (Html.BeginForm(new {})) 
    {%> 
    <%= Html.AntiForgeryToken() %> 
<%: Model.Id %> 
<%= Html.HiddenFor(e=>e.SelectedBrands) %> 

<p> 
<%= Html.LabelFor(e=>e.Name,"Name:") %> 
<%= Html.TextBoxFor(e => e.Name, new { @style = "width: 255px;" })%> 
<%= Html.ValidationMessageFor(e=>e.Name,"") %> 
</p> 
<p> 
<%= Html.LabelFor(e=>e.Email,"Email:") %> 
<%= Html.TextBoxFor(e => e.Email, new { @style = "width:250px;" })%> 
<%= Html.ValidationMessageFor(e=>e.Email,"") %> 
</p> 
<p> 
<%= Html.LabelFor(e=>e.Corporation.Description,"Corporation:") %> 
<%= Html.TextBoxFor(e => e.Corporation.Description, new { @style = "width:250px;" })%> 
<%= Html.ValidationMessageFor(e=>e.Corporation.Description,"") %> 
</p> 
    <hr /> 
<h2>Approve User</h2> 
    <p> 
<%= Html.LabelFor(e=>e.Approve,"Aprrove User:") %> 
<%= Html.CheckBoxFor(e=> e.Approve,Model.Approve) %><span>&nbsp;Switch this on if you 
    want to 
    add regitrar </span> 
<%= Html.ValidationMessageFor(e=>e.Approve,"") %> 
</p> 
    <hr /> 
<h2>Multi Select Brands If Required:</h2> 
<div id="checkboxes"> 
<% foreach (var manufacturer in Model.Manufacturer) 
    {%> 
<input type="checkbox" id="<%: manufacturer.Id %>" class="checkbox" value="<%: 
manufacturer.Description %>" /><%: manufacturer.Description %><br /> 
<%} %> 
<input type="submit" value="UpdateBrands" class="brands" /> 
</div> 
<div id="checkboxescountries" class="divNewCountriesList"> 

</div> 
<hr /> 
<p> 
<input type="submit" value="Save" /> 
<%= Html.ActionLink("Cancel","Details",new {id=Model.Id},new {@title="exit without 
saving"}) %> 
</p> 
<% } %> 


    <script type="text/javascript"> 
    $(".brands").click(function(event) { 
    event.preventDefault(); 
    var selected = ""; 
    var manu = ""; 
    var input = ""; 
    $("#checkboxes input:checked").each(function() { 

     manu = $(this).val(); 
     selected = selected + "," + manu; 
     alert(selected); 
     $("#SelectedBrands").val(selected); 
     alert($("#SelectedBrands").val()); 

    }); 
    var productInput = ""; 
    alert("I am here"); 
    var myUrl = "/Countries/GetCountiresForManufacturer/" + selected; 
    alert(myUrl); 
    $.ajax({ 
     url: myUrl, 
     type: 'get', 
     success: function(data) { 
      productInput = data; 
      alert(productInput); 
      //$('.brands').after("<div id='divNewCountriesList" + id + "' class ='divNewCountriesList'"+" </div>"); 

      } 
    }); 
    $(".divNewCountriesList").html(productInput); 

}); 

참고 : 내가 줄 경우

$(".divNewCountriesList").html("Hello World"); 

div에서는 볼 수 있지만 productInput에서는 볼 수 없습니다. 내가 어디에서 잘못하고 있는지 확실하지 않습니다.

답변

2

Ajax는 비동기 요청입니다. 그래서 ajax 성공 처리기 안에 div의 html을 설정해야합니다.

$.ajax({ 
     url: myUrl, 
     type: 'get', 
     success: function(data) { 
      productInput = data; 
      alert(productInput); 
      $(".divNewCountriesList").html(productInput); 
     } 
    }); 
+0

많은 감사를 보냈습니다. 곧 답변으로 표시됩니다. – 62071072SP