Partial view render on button click

后端 未结 2 341
长发绾君心
长发绾君心 2021-01-02 04:52

I have Index view:

@using System.Web.Mvc.Html
@model  MsmqTestApp.Models.MsmqData



    

        
2条回答
  •  误落风尘
    2021-01-02 05:20

    The first thing to do is to reference jQuery. Right now you have referenced only jquery.unobtrusive-ajax.min.js but this script has dependency on jQuery, so don't forget to include as well before it:

    
    
    

    Now to your question: you should use submit buttons with an HTML form. In your example you don't have a form so it would be semantically more correct to use a normal button:

    
    
    

    and then in a separate javascript file AJAXify those buttons by subscribing to the .click() event:

    $(function() {
        $(':button').click(function() {
            $.ajax({
                url: $(this).data('url'),
                type: 'GET',
                cache: false,
                success: function(result) {
                    $('#msmqpartial').html(result);
                }
            });
            return false;
        });
    });
    

    or if you want to rely on the Microsoft unobtrusive framework you could use AJAX actionlinks:

    @Ajax.ActionLink("Buy", "BuyItem", "MsmqTest", new { area = "Msmq" }, new AjaxOptions { UpdateTargetId = "msmqpartial" })
    @Ajax.ActionLink("Sell", "SellItem", "MsmqTest", new { area = "Msmq" }, new AjaxOptions { UpdateTargetId = "msmqpartial" })
    

    and if you want buttons instead of anchors you could use AJAX forms:

    @using (Ajax.BeginForm("BuyItem", "MsmqTest", new { area = "Msmq" }, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
    {
        
    }
    @using (Ajax.BeginForm("SellItem", "MsmqTest", new { area = "Msmq" }, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
    {
        
    }
    

    From what I can see you have already included the jquery.unobtrusive-ajax.min.js script to your page and this should work.

提交回复
热议问题