mvc模型null引用

时间:2013-09-04 12:36:34

标签: c# asp.net-mvc asp.net-mvc-3

在我的网络应用程序中发生了一些奇怪的事情,我无法找到原因。请帮助我或者让我知道如何解决这个挑战。相同的代码在某些情况下适用于另一个我得到空引用异常。 在控制器中,我从DB获得特定类别的模型。然后,在视图中我列出了此类别中的所有产品。当我从列表中选择一些产品并按“购物车”按钮时,有时模型会正确传递/传输到控制器,有时模型中的产品为空。

我的控制器操作:

    [HttpGet]
    [PacCheckPermissionAttribute(PacPermissionRecordEnum.perm_pubmatorder)]
    public ActionResult PacCategoryDetails(int categoryId)
    {
        Category category = _categoryService.GetCategoryById(categoryId);
        if(category == null || category.Deleted || !category.Published)
            return RedirectToRoute("HomePage");


        CategoryModel model = prepareCategoryModel(category, false);
        return View(model);
    }

    [HttpPost]
    [PacCheckPermissionAttribute(PacPermissionRecordEnum.perm_pubmatorder)]
    public ActionResult PacCategoryDetails(CategoryModel model)
    {
        Category category = _categoryService.GetCategoryById(model.Id);
        if (category == null || category.Deleted || !category.Published)
            return RedirectToRoute("HomePage");

        //Add the selected Products to ShoppingCart
        for (int i = 0; i < model.Products.Count; i++)
        ...

model.Products有时是null,有时是正确的 - 不知道为什么??? 我的观点:

    @model CategoryModel
    @using Nop.Web.Models.Catalog;
    @using Nop.Core.Domain.Orders;
    @using Nop.Web.Framework;
    @using Nop.Web.Models.ShoppingCart;
    @using PacControls;
    @using Telerik.Web.Mvc.UI;
    @{
        Html.AppendScriptParts(@Url.Content("~/Administration/Scripts/jquery.validate.unobtrusive.min.js"));
        Html.AppendScriptParts(@Url.Content("~/Administration/Scripts/jquery.validate.min.js"));
        Html.AppendScriptParts(@Url.Content("~/Administration/Scripts/jquery.unobtrusive-ajax.min.js"));
        Layout = "~/Views/Shared/_PacLoggedIn.cshtml";
    }
    @section SideBar
    {
        @Html.Action("PacCategoryNavigation", "Catalog", new { currentCategoryId = Model.Id})
    }
    @using (Html.BeginForm())
    {
        <div class="productdetails-page">
            <div class="body">
                <div class="info-row">
                    <div class="productlist-content">
                        <div class="info-row">
                            <div class="box border-r box-s info-row category-details">
                                <!-- <div class="f-l" style="width: 320px;">@Html.Partial("_ProductDetailsPictures", Model)</div> -->
                                <div class="f-l" style="width: 360px;"><h1>@Html.DisplayTextFor(x => Model.Name)</h1>
                                                 @Html.Raw(Model.Description)
                                                 @Html.HiddenFor(x => Model.Id)
                                </div>
                                <div class="f-c"></div>
                            </div>
                            <div class="f-c"></div>
                        </div>
                        <div class="f-c"></div> 
                        @if (Model.Products.Count > 0)
                        {
                            <div class="titelbox border-top-r-l full-row darkgrey-background">
                                <div class="page-title">
                                    <h3>@T("Pac.PromotionMaterial")</h3>
                                </div>
                            </div>
                            <div class="box border-bottom-r-l info-row">
                                <div class="osc-data">
                                    <div class="osc-table">
                                        <div class="mm-items">
                                        @for (int i = 0; i < Model.Products.Count; i++)
                                        {
                                            if (!Model.Products[i].IsDownload)
                                            {
                                                <div class="mm-row" id="@(i)">
                                                    @Html.HiddenFor(x => Model.Products[i].Id)
                                                    <div class="mm-col quanity-col">
                                                        <div>
                                                            @Html.EditorFor(x => Model.Products[i].Quantity)
                                                            @Html.ValidationMessageFor(x => Model.Products[i].Quantity)
                                                        </div>     
                                                    </div>
                                                    <div class="mm-col name-col">
                                                        <div id="name_@(i)">
                                                            @Html.DisplayTextFor(x => Model.Products[i].Name)
                                                        </div>
                                                    </div>
                                                    <div class="mm-col price-col">
                                                        <div>
                                                            <div id="price_@(i)" class="price-col">@Html.DisplayTextFor(x => @Model.Products[i].ProductPrice.Price)</div> 
                                                            <input type="hidden" id="pricevalue_@(i)" value="@(Model.Products[i].ProductPrice.PriceValue)"/>
                                                        </div>    
                                                    </div>
                                                    <div class="f-c"></div>
                                                </div>
                                            }
                                        }
                                        </div>
                                    </div>
                                </div>
                            </div>
                        }
                            @if (Model.Products.Count > 0)
                            {
                            <div class="titelbox border-top-r-l full-row darkgrey-background">
                                <div class="page-title">
                                    <h3>@T("Pac.Download")</h3>
                                </div>
                            </div>
                            <div class="box border-bottom-r-l info-row">
                                <div class="osc-data">
                                    <div class="osc-table">
                                        <div class="mm-items">
                                        @for (int i = 0; i < Model.Products.Count; i++)
                                        {
                                            if (Model.Products[i].IsDownload)
                                            {
                                                <div class="mm-row" id="@(i)">
                                                    <div class="mm-col downloadname-col">
                                                        <div>
                                                            @Html.DisplayTextFor(x => Model.Products[i].Name)
                                                        </div>
                                                    </div>
                                                    <div class="mm-col downloadbutton-col">
                                                        @if (Model.Products[i].IsDownload)
                                                        {
                                                            @Html.SmallButton(T("Pac.Button.Download").Text, ButtonStyle.Default, "location.href = '" +Url.RouteUrl("GetProductDownload", new { productvariantid = Model.Products[i].ProductVariantId }) + "';return false;" , new Dictionary<string, object>() { { "class", "smallbuttoncart" } })
                                                        }
                                                    </div>
                                                    <div class="f-c"></div>
                                                </div>
                                            }
                                        }
                                        </div>
                                    </div>
                                </div>
                            </div>
                            }
                            <div class="titelbox border-top-r-l full-row darkgrey-background">
                                <div class="page-title darkgrey-background">
                                    <h3>@T("Pac.Summary")</h3>
                                </div>
                            </div>
                            <div class="box border-bottom-r-l info-row">
                                <div class="osc-data">
                                    <div class="osc-table">
                                        <div class="mm-items" id="summarytable">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="f-c"></div>  
                        <div class="f-r full-row right">
                        @Html.SubmitButton(T("Pac.Button.ToShoppingCart").Text, ButtonStyle.Lightgrey)
                        </div>
                        <div class="f-c "></div>
                </div>
            </div>
        </div>
    }
<script type="text/javascript" language="javascript">

    var summaryrow = "<div class=\"mm-row mm-summaryrow\"><div class=\"mm-col summaryquantity-col\" id=\"summaryrow\"></div><div class=\"mm-col summaryname-col\">@T("Pac.Price")</div><div class=\"mm-col summaryprice-col\">#price#</div><div class=\"f-c\"></div></div>";
    var elementrow = "<div class=\"mm-row\" id=\"summaryrow_\"><div class=\"mm-col summaryquantity-col\" id=\"summaryquanity_\">#quantity#</div><div class=\"mm-col summaryname-col\" id=\"summaryname_\">#name#</div><div class=\"mm-col summaryprice-col\" id=\"summaryprice_\">#price#</div><div class=\"f-c\"></div></div>";

    function calculateSummary() {
        var sum = 0.00;
        var position = 0;

        $('#summarytable').empty();

        $('input[id$="Quantity"]').each(
            function () {

                //summ price
                var id = this.id.substring(this.id.indexOf("[")+1, this.id.indexOf("]"));
                var price = $('#pricevalue_' + id)[0].value.replace(",",".");
                var quantity = this.value;
                var val = 0;
                if(!isNaN(quantity) && !isNaN(price) && quantity > 0)
                {
                    val = parseFloat(quantity) * parseFloat(price);
                    sum = sum + val;

                    //Create new Row
                    var newrow = elementrow;
                    newrow = newrow.replace("#quantity#", this.value);
                    newrow = newrow.replace("#name#", $('#name_' + id)[0].innerHTML);
                    newrow = newrow.replace("#price#", val.toFixed(2).replace(".",",") + " (EUR)");
                    newrow = newrow.replace("summaryrow_", "summaryrow_" + id);
                    newrow = newrow.replace("summaryquanity_", "summaryquanity_" + id);
                    newrow = newrow.replace("summaryname_", "summaryname_" + id);
                    newrow = newrow.replace("summaryprice_", "summaryprice_" + id);

                    //add row to summarytable
                    $('#summarytable').append(newrow);
                }
            }
            );

        var newsummaryrow = summaryrow;
        newsummaryrow = newsummaryrow.replace("#price#", sum.toFixed(2).replace(".",",") + " (EUR)");
        $('#summarytable').append(newsummaryrow);
    }

    function addOnUpdateFunctions()
    {
        $('input[id$="Quantity"]').each(
            function () {
                this.setAttribute("onchange", "calculateSummary();");
                });
        $("a[class^='t-link t-icon t-arrow-']").each(
            function () {
                this.setAttribute("onclick", "calculateSummary();");
                });
    }

    $(document).ready(calculateSummary());
    $(document).ready(addOnUpdateFunctions());
</script>

@(Html.Telerik().StyleSheetRegistrar()
        .DefaultGroup(group => group
            .Add("telerik.common.min.css")
            .Add("telerik.vista.min.css")
            .Add("telerik.rtl.min.css")
            ))
@(Html.Telerik().ScriptRegistrar()
        .jQuery(false)
        .jQueryValidation(false)
        )

知道发生了什么事吗?感谢名单!

1 个答案:

答案 0 :(得分:0)

您有两个条件阻止产品列在<form>

if (Model.Products.Count > 0)

if (Model.Products[i].IsDownload)

因此,如果类别中没有产品,则没有可下载的产品,那么Products在发布时将为null 。这是预期的行为。只需检查null并执行您需要执行的操作。在这种情况下,我会说你可能只是忽略它,因为没有产品可以添加到购物车中,而且用户可能只是错误地点击了它。