destructi6n destructi6n - 2 months ago 9
C# Question

Rendering Partial not being rendered .NET mvc

I'm running into an issue in rendering partials. I've looked around and found you're supposed to pass the path to the partial view as the first parameter, and your model to the second parameter if you're using one since it's one of the overloads. I'm getting the page, without anything blowing up, but the partial view is not outputting my expected results, and I cannot hit a breakpoint on that partial (It's using a loop). I've confirmed I'm passing it a model with data, but the partial doesn't seem to be getting hit. Here is my controller and partials.

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");
}


And here is the partial view that I'm having troubles with. This is what's not getting hit.

@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>


And here is my view that's calling the renderpartial method to the ChooseExistingAddress.cshtml partial shown above.

@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");
}

Answer

You've got your ternary logic backwards:

x.HasExistingAddresses = (AvailableAddresses.Count > 0 ? false : true);

In other words, if count of addresses is greater than 0, you're setting it to false, whereas, it should be true, in that case. You don't really need a ternary, here, anyways. Just set it to the result of the boolean expression:

x.HasExistingAddresses = AvailableAddresses.Count > 0;
Comments