![](/img/trans.png)
[英]Child action/partial view is not being rendered when called in asp.net mvc 2 application
[英]Rendering Partial not being rendered .NET mvc
我在渲染局部時遇到了一個問題。 我環顧四周,發現您應該將路徑傳遞給局部視圖作為第一個參數,將模型傳遞給第二個參數(如果您使用的是第二個參數,因為這是重載之一)。 我正在獲取頁面,沒有任何異常,但是局部視圖沒有輸出預期的結果,並且我無法在該局部視圖上遇到斷點(它正在使用循環)。 我已經確認我正在向它傳遞一個帶有數據的模型,但是局部模型似乎並沒有受到影響。 這是我的控制器和部件。
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>
這是我的視圖,它向上述的ChooseExistingAddress.cshtml部分調用renderpartial方法。
@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);
換句話說,如果地址計數大於0,則將其設置為false,在這種情況下,它應該為true。 無論如何,您實際上並不需要三元組。 只需將其設置為布爾表達式的結果即可:
x.HasExistingAddresses = AvailableAddresses.Count > 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.