部分视图呈现按钮单击

时间:2012-08-14 21:53:41

标签: asp.net asp.net-mvc partial-views

我有索引视图:

@using System.Web.Mvc.Html
@model  MsmqTestApp.Models.MsmqData
<!DOCTYPE html>
<html>
<head>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <meta name="viewport" content="width=device-width" />
    <title>MsmqTest</title>
</head>
<body>
    <div>
        <input type="submit" id="btnBuy" value="Buy" onclick="location.href='@Url.Action("BuyItem", "MsmqTest", new { area = "Msmq" })'" />
        <input type="submit" id="btnSell" value="Sell" onclick="location.href='@Url.Action("SellItem", "MsmqTest", new { area = "Msmq" })'" />
    </div>
    <div id="msmqpartial">
    @{Html.RenderPartial("Partial1", Model); }

    </div>
</body>
</html>

和部分:

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

    <p>
        Items to buy
        @foreach (var item in Model.ItemsToBuy)
        { 
            <tr>
                <td>@Html.DisplayFor(model => item)
                </td>
            </tr>
        }
    </p>
    <p>
        <a>Items Selled</a>
        @foreach (var item in Model.ItemsSelled)
        { 
            <tr>
                <td>@Html.DisplayFor(model => item)
                </td>
            </tr>
        }
    </p>

和控制器:

 public class MsmqTestController : Controller
    {
        public MsmqData data = new MsmqData();

        public ActionResult Index()
        {

            return View(data);
        }

        public ActionResult BuyItem()
        {
            PushIntoQueue();
            ViewBag.DataBuyCount = data.ItemsToBuy.Count;
            return PartialView("Partial1",data);
        }
}

如果我点击其中一个按钮只是部分视图渲染,现在控制器想要将我移动到BuyItem视图; /

2 个答案:

答案 0 :(得分:20)

首先要做的是引用jQuery。现在你只引用了jquery.unobtrusive-ajax.min.js但是这个脚本依赖于jQuery,所以不要忘记在它之前包含它:

<script src="@Url.Content("~/Scripts/jquery.jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

现在提出您的问题:您应该使用带有HTML表单的提交按钮。在您的示例中,您没有表单,因此使用普通按钮在语义上更正确:

<input type="button" value="Buy" data-url="@Url.Action("BuyItem", "MsmqTest", new { area = "Msmq" })" />
<input type="button" value="Sell" data-url="@Url.Action("SellItem", "MsmqTest", new { area = "Msmq" })" />

然后在单独的javascript文件中通过订阅.click()事件来AJAXify这些按钮:

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

或者如果您想依赖Microsoft不引人注目的框架,您可以使用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" })

如果你想要按钮而不是锚,你可以使用AJAX表格:

@using (Ajax.BeginForm("BuyItem", "MsmqTest", new { area = "Msmq" }, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{
    <button type="submit">Buy</button>
}
@using (Ajax.BeginForm("SellItem", "MsmqTest", new { area = "Msmq" }, new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{
    <button type="submit">Sell</button>
}

从我看到的内容中,您已将jquery.unobtrusive-ajax.min.js脚本包含在您的网页中,这应该有用。

答案 1 :(得分:1)

也许不是您正在寻找的解决方案但是,我会忘记部分并使用Javascript调用服务器来获取所需的数据,然后将数据作为JSON返回给客户端并使用它将结果呈现给页面异步。

JavaScript函数;

var MyName = (function () {


//PRIVATE FUNCTIONS
var renderHtml = function(data){
   $.map(data, function (item) {
       $("<td>" + item.whateveritisyoureturn + "</td>").appendTo("#msmqpartial");
   });
};

//PUBLIC FUNCTIONS
var getData = function(val){
   // call the server method to get some results.
    $.ajax({ type: "POST",
        url: "/mycontroller/myjsonaction",
        dataType: "json",
        data: { prop: val },
        success: function (data) {
            renderHtml();
        },
        error: function () {
        },
        complete: function () {
        }
    });
};

//EXPOSED PROPERTIES AND FUNCTIONS
return {
    GetData : getData
};


})();

在服务器上....

public JsonResult myjsonaction(string prop)
        {
            var JsonResult;

            // do whatever you need to do

            return Json(JsonResult);
        }
希望这会有所帮助......