渲染部分没有被渲染.net mvc
本文关键字:net mvc 染部 | 更新日期: 2023-09-27 18:05:49
我在渲染局部时遇到了一个问题。我看了一下,发现你应该把路径传递给局部视图作为第一个参数,你的模型传递给第二个参数,如果你使用一个,因为它是重载之一。我得到了页面,没有任何问题,但是部分视图没有输出我预期的结果,而且我不能在该部分上碰到断点(它使用了循环)。我已经确认给它传递了一个有数据的模型,但部分似乎没有被击中。这是我的控制器和偏导数。
DataBass db = new DataBass();
public ActionResult Address()
{
Models.ViewModels.CheckoutViewModel x = new Models.ViewModels.CheckoutViewModel();
if (User.Identity.IsAuthenticated)
{
using(DataBass oDB = new DataBass())
{
var Customer = oDB.Customers.First(c => c.Email.ToLower() == User.Identity.Name.ToLower());
var AvailableAddresses = oDB.AddressBooks.Where(ab => ab.CustomerID == Customer.ID).ToList();
x.CustomerID = Customer.ID;
x.HasExistingAddresses = (AvailableAddresses.Count > 0 ? false : true);
x.AvailableAddresses = AvailableAddresses;
}
//var UserAddresses = db.AddressBooks.Where(a => a.Customer.Email == User.Identity.Name);
//ViewBag.Addresses = UserAddresses;
return View("Shipping", x);
}
else
return View("Index");
}
这是我遇到麻烦的部分视图。这是不会被击中的。
@model IEnumerable<Models.AddressBook>
<h2>AddressBook</h2>
<table class="table">
<tr>
<th>
Address 2
</th>
<th>
Address 1
</th>
<th>
City
</th>
<th>
State
</th>
<th>
Postal Code
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
var states = (List<Models.State>)ViewBag.States;
<tr>
<td>
@Html.DisplayFor(modelItem => item.Address1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address2)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@states.FirstOrDefault(x => x.ID.ToString() == item.StateID).Name
</td>
<td>
@Html.DisplayFor(modelItem => item.Zipcode)
</td>
<td>
@Html.ActionLink("Choose this one","", new { id = item.ID}, new { @class="button"})
@*@Html.ActionLink("Edit", "EditAddress", new { id = item.ID }, null) |
@Html.ActionLink("Delete", "DeleteAddress", new { id = item.ID }, null)
@*<a href="/Account/EditAddress/@item.AddressBook_ID">Edit</a> |
<a href="/Account/DeleteAddress/@item.AddressBook_ID">Delete</a>
@Html.ActionLink("Edit", "EditAddress", new { id = item.ID }, null) |
@Html.ActionLink("Edit", "EditAddress", "Account",new { id=item.AddressBook_ID }, htmlAttributes: null ) |
@Html.ActionLink("Delete", "DeleteAddress", new { id = item.ID }, null)*@
</td>
</tr>
}
</table>
这是我的视图它调用renderpartial方法到ChooseExistingAddress。CSHTML部分如上所示。
@model Models.ViewModels.CheckoutViewModel
@{
ViewBag.Title = "Shipping Selection";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Shipping</h2>
@if (Model.HasExistingAddresses)
{
Html.RenderPartial("~/Views/Checkout/ChooseExistingAddress.cshtml", Model.AvailableAddresses);
}
else
{
Html.RenderPartial("~/Views/Checkout/AddNewShippingAddress.cshtml");
}
你把你的三元逻辑弄反了:
x.HasExistingAddresses = (AvailableAddresses.Count > 0 ? false : true);
换句话说,如果count of addresses大于0,你将其设置为false,而在这种情况下,它应该是true。反正这里不需要三元制。只需将其设置为布尔表达式的结果:
x.HasExistingAddresses = AvailableAddresses.Count > 0;