weixin_33704234 2014-08-05 13:09 采纳率: 0%
浏览 13

未进行MVC 4 Ajax调用

I've been trying to fix a problem in my app for a couple of days now and I can't figure out what's happening.

I'm developing a MVC 4 application. I have a view in which there's a div that I load with more html with an ajax call that is executed on the $(function() { ... });. That ajax call works fine.

Problems start when I make the second ajax call. I paste the code below :-

In the main view :-

<div class="body" id="loadedData">

</div>

<script type="text/javascript" charset="utf-8">
   $(function(){
      loadData("@Url.Action("Authorizations","User", new { id = Model.ID })");
   });

   function loadData(url){
      $.ajax({
        type: 'GET',
        url: url,
        success: function (data) {
            $("#loadedData").html(data);
        }
    });
   }
</script>

And the partial view that is loaded in the div it's this:

@if (ViewBag.UserAuths != null){
  <table>
      <tr>
          <th>
              Operations
          </th>
          <th></th>
      </tr>
      @foreach (var prod in ViewBag.UserAuths){
          <tr>
              <td>
                  @prod[0]
              </td>
              <td>
                 <a href="" onclick="loadData(@Url.Action("RevokeAccess", "User", new {    id = ViewBag.UserID, op = Int32.Parse(prod[1])}));">Remove</a>
              </td>
          </tr>
      }
  </table>
 }

The problem happens when I click on the link in the HTML that's loaded by ajax (Remove). When i click, the page 'blinks' but nothing happens. I've put a breakpoint in the RevokeAccess function in UserController and it never stops.

Please help!

Edit:

There's other thing, when i click on the link, in the Chrome console it's shown a "Uncaught SyntaxError: Unexpected token )" message, but it disappears very quickly, i don't know why.

  • 写回答

1条回答 默认 最新

  • weixin_33709590 2014-08-05 13:24
    关注

    As you are using jQuery don't use inline events. Bind click event using jQuery. As you are fetching Partial View you can use .load() which is much simpler.

    Script

    <script type="text/javascript">
       $(function(){
           $("#loadedData").load("@Url.Action("Authorizations","User", new { id = Model.ID })");      
    
           $("#loadedData").on('click', '.anchor', function(event){
                event.preventDefault();//Stop default action  
                $("#loadedData").load(
                    $(this).data('url'),  //Url
                    { id: $(this).data('id'), op : $(this).data('op')}, //Parameters
                    function(){
                        //Perform any action if any
                    }
                );
           })
       });
    
    </script>
    

    Change Your Partials as

    @if (ViewBag.UserAuths != null){
      <table>
          <tr>
              <th>
                  Operations
              </th>
              <th></th>
          </tr>
          @foreach (var prod in ViewBag.UserAuths){
              <tr>
                  <td>
                      @prod[0]
                  </td>
                  <td>
                     <a class='anchor' href="#" data-url='@Url.Action("RevokeAccess", "User")' data-id="@ViewBag.UserID" data-op="@Int32.Parse(prod[1])">Remove</a>
                  </td>
              </tr>
          }
      </table>
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀