渲染部分未呈现.NET mvc

时间:2016-09-06 17:30:36

标签: c# asp.net-mvc asp.net-mvc-4 razor view

我在渲染局部问题时遇到了问题。我环顾四周,发现你应该将路径作为第一个参数传递给局部视图,如果你使用了一个参数,那么你的模型会传递到第二个参数,因为它是一个参数重载。我得到了这个页面,没有任何东西爆炸,但部分视图没有输出我的预期结果,我不能在那个部分(它使用循环)上找到一个断点。我已经确认我已经传递了一个包含数据的模型,但是部分似乎没有被击中。这是我的控制器和部分。

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

1 个答案:

答案 0 :(得分:3)

你的三元逻辑倒退了:

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

换句话说,如果地址数大于0,则将其设置为false,而在这种情况下应该为真。无论如何,你真的不需要三元组。只需将其设置为布尔表达式的结果:

x.HasExistingAddresses = AvailableAddresses.Count > 0;