gpt4 book ai didi

asp.net-mvc-3 - 如何只重新加载页面的一部分

转载 作者:行者123 更新时间:2023-12-04 06:30:37 24 4
gpt4 key购买 nike

我的网购页面,包含两部分。第一部分是产品列表。

@model WebUI.Models.CakesListViewModel
@foreach (var p in Model.Cakes) {
Html.RenderPartial("CakesList", p);
}

每个产品都显示为局部 View 。蛋糕列表.cshtml

@model Domain.Entities.Cake
<div>
@if (Model.ImageData != null) {
<div style="float:left;margin-right:20px">
<img width="75" height="75" src="@Url.Action("GetImage", "Cake", new { Model.Id })" />
</div>
}
<b>@Model.Name<br />
@Model.Price</b><br />
@Model.Description
@using (Html.BeginForm("AddToCart", "Cart"))
{
@Html.HiddenFor(x => x.Id)
@Html.TextBox("quantity", "", new { style = "width: 20px; text-align: center" })
<input type="image" src="../Images/basket.jpg" />
}
</div>

点击篮子图片按钮后,所有页面都在重新加载,但我只需要重新加载页面的第二部分。我该怎么做。第二部分是订购产品的总和。

@model Domain.Entities.Cart
@{
Layout = null;
string str = String.Format("{0}", Model.ComputeTotalValue());
}
<div id="cart">
@Html.ActionLink(str, "Index", "Cart")
</div>

出现在_Layout.cshtml

</head>
<body>
<div id="header">
@{Html.RenderAction("Summary", "Cart");}
<div class="title">Cakes</div>
</div>
<div id="categories">
@{ Html.RenderAction("Menu", "MenuItems"); }
</div>
<div id="content">
@RenderBody()
</div>
</body>
</html>

最佳答案

//蛋糕 Controller

namespace WebUI.Controllers
{
public class CakeController : Controller
{
public int PageSize = 4;
private ICakeRepository repository;
public CakeController(ICakeRepository cakeRepository)
{
repository = cakeRepository;
}
public FileContentResult GetImage(int id)
{
Cake prod = repository.Cakes.FirstOrDefault(p => p.Id == id);
if (prod != null)
return File(prod.ImageData, prod.ImageMimeType);
else
return null;
}
public ActionResult OnlineShop(string regions, int page = 1)
{
CakesListViewModel viewModel = new CakesListViewModel
{
Cakes = repository.Cakes
.Where(p => regions == null || p.Name.Trim() == regions),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = 1
},
CurrentCategory = "category"
};
return View("OnlineShop", viewModel);
}
}
}

//OnlineShop.cshtml

@model WebUI.Models.CakesListViewModel
@{
ViewBag.Title = "Cakes";
}
@foreach (var p in Model.Cakes) {
Html.RenderPartial("CakesList", p);
}

//蛋糕列表.cshtml

@model Domain.Entities.Cake
@if (Model.ImageData != null) {
<div style="float:left;margin-right:20px">
<img width="75" height="75" src="@Url.Action("GetImage", "Cake", new { Model.Id })" />
</div>
}
<b>@Model.Name<br />
@Model.Price</b><br />
@Model.Description
@using (Ajax.BeginForm("AddToCart", "Cart", new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "mydiv" }))
{
@Html.HiddenFor(x => x.Id)
@Html.TextBox("quantity", "", new { style = "width: 20px; text-align: center" })
<input type="image" src="../Images/basket.jpg" />
}

//购物车 Controller

namespace WebUI.Controllers
{
public class CartController : Controller
{
private ICakeRepository repository;
private IOrderProcessor orderProcessor;
public CartController(ICakeRepository repo, IOrderProcessor proc)
{
repository = repo;
orderProcessor = proc;
}
public ActionResult AddToCart(Cart cart, int id, int quantity = 1)
{
Cake cake = repository.Cakes.FirstOrDefault(p => p.Id == id);
if (cake != null)
cart.AddItem(cake, quantity);
return RedirectToAction("OnlineShop", "Cake");
}
public ActionResult RemoveFromCart(Cart cart, int id)
{
Cake cake = repository.Cakes.FirstOrDefault(p => p.Id == id);
if (cake != null)
cart.RemoveLine(cake);
return RedirectToAction("Index", "Cart");
}
public ViewResult Index(Cart cart, string returnUrl)
{
return View(new CartIndexViewModel
{
Cart = cart,
ReturnUrl = returnUrl
});
}
public ViewResult Summary(Cart cart)
{
return View(cart);
}
}
}

//总结.cshtml

@model Domain.Entities.Cart
@{
Layout = null;
string str = String.Format("{0}", Model.ComputeTotalValue());
}
<div id="cart">
<p>
<img src="../Images/basket.jpg" />
<div id="mydiv">@Html.ActionLink(str, "Index")</div>
</p>
</div>

关于asp.net-mvc-3 - 如何只重新加载页面的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9905103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com