asp.net – 部分视图呈现按钮点击
发布时间:2021-01-11 11:52:21 所属栏目:asp.Net 来源:互联网
导读:我有索引视图: @using System.Web.Mvc.Html@model MsmqTestApp.Models.MsmqData!DOCTYPE htmlhtmlhead script src=@Url.Content(~/Scripts/jquery.unobtrusive-ajax.min.js) type=text/javascript
|
我有索引视图: @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",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视图; / 解决方法首先要做的是引用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",new { area = "Msmq" })" />
<input type="button" value="Sell" data-url="@Url.Action("SellItem",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",new { area = "Msmq" },new AjaxOptions { UpdateTargetId = "msmqpartial" })
@Ajax.ActionLink("Sell","SellItem",new AjaxOptions { UpdateTargetId = "msmqpartial" })
如果您想要按钮而不是锚点,您可以使用AJAX表单: @using (Ajax.BeginForm("BuyItem",new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{
<button type="submit">Buy</button>
}
@using (Ajax.BeginForm("SellItem",new AjaxOptions { UpdateTargetId = "msmqpartial" }))
{
<button type="submit">Sell</button>
}
从我可以看到,您已经将jquery.unobtrusive-ajax.min.js脚本包含在您的页面中,这应该是正常的. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 剃刀引擎 – 如何根据不同的条件渲染不同
- asp.net – 使用AJAX进行WCF调用
- asp.net 文件上传实例汇总
- ASP.NET:隐藏gridview中的列
- kendo-ui – 剑道网格刷新(数据绑定两次)
- asp.net-core – 构建asp.net核心错误
- asp.net-mvc – 在ASP.NET MVC中实现工作单元的方法
- asp.net-mvc – ASP.NET MVC如何在生产中禁用调试路由/视图
- asp.net – 是否可以在源代码中使用iframe和localhost地址?
- 来自ASP.NET MVC站点的“无效的JSON原语:alihack”错误
推荐文章
站长推荐
热点阅读
