1

突然我的创建操作/视图开始导致 NullReferenceException。实际上它是在迁移到 bootstrap twitter 3 之后开始的。

这是导致异常的第一行:

@Html.LabelFor(model => model.Name, new { @class = "col-lg-2 control-label" })

这是我的控制器代码:

        //
        // GET: /Panel/Partners/Create/
        [Authorize]
        public ActionResult Create()
        {
            ViewData["Logo"] = "";

            return View();
        }

局部视图(表格):

@model MyApp.Entities.Partner

<fieldset data-listurl="@Url.Action("Index", "Partners", new { Area = "Panel" })" data-removelogo="@Url.Action("RemoveLogo", "Partners", new { Area = "Panel" })">
    <legend>Partners</legend>


    <div class="form-group">
        @Html.LabelFor(model => model.Name, new { @class = "col-lg-2 control-label" })
        <div class="col-lg-10">
            <div class="col-lg-7">
                <input type="text" id="Name" name="Name" value="@Model.Name" class="form-control" />
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Logo, new { @class = "col-lg-2 control-label" })
        <div class="col-lg-10">
            <div id="file-upload">
                <div class="col-lg-9">
                    <span class="">@(ViewData["Logo"] == null || String.IsNullOrWhiteSpace(ViewData["Logo"].ToString()) ? "No file" : ViewData["Logo"].ToString()) </span>

                    <a href="#" class="btn btn-primary select-button">Choose</a>
                </div>
                <input type="file" name="fileUpload" id="fileUpload" style="left: 0; top: -5000px; position: absolute;" />
            </div>@Html.HiddenFor(model => model.Logo)
            <div class="col-lg-9">
                @if (ViewData["Logo"] != null && !String.IsNullOrWhiteSpace(ViewData["Logo"].ToString()))
                {
                    <p><img src="@String.Concat("http://remotepark.blob.core.windows.net/pictures/", ViewData["Logo"])" id="logoView" class="img-thumbnail" style="max-height: 150px" /></p>
                    <p><a href="#" class="btn btn-default remove-button">Remove</a></p>
                }
            </div>
        </div>
    </div>

    <div class="row form-actions">
        <div class="col-lg-12">
            <input type="submit" id="btnSubmit" class="btn btn-primary" value="Save" />
            <input type="button" class="btn" id="btnCancelar" value="Cancel" />
        </div>
    </div>
</fieldset>

创建.cshtml

@model MyApp.Entities.Partner

<div class="panel-middle-top">
    <div class="middle-top-left"></div>
    <div class="middle-top-center">
        <ul class="breadcrumb" style="background-color: transparent;">
            <li><a href="@Url.Action("Index", "Home", new { Area = "Panel" })">Home</a></li>
            <li><a href="@Url.Action("Index", "Partners", new { Area = "Panel" })">Partners</a></li>
            <li class="active">New</li>
        </ul>
    </div>
    <div class="middle-top-right"></div>
</div>
<div class="panel-middle-content">
    @Html.Partial("/Areas/Panel/Views/Shared/_Alerts.cshtml")

    @using (Html.BeginForm("Create", "Partners", 
        FormMethod.Post, 
        new { 
            enctype = "multipart/form-data", 
            id = "partnersForm",
            @class="form-horizontal", 
            rule="form"
        }))
    {
        @Html.Partial("_PartnersForm")
    }
</div>

模型类

public partial class Partner
    {
        #region Primitive Properties

        public int ID { get; set; }

        public string Name { get; set; }

        public string Logo { get; set; }

        /// <summary>
        /// Indicater whether this partner is associated to the current parking lot
        /// </summary>
        public bool Associated { get; set; }

        #endregion

        #region Navigation Properties

        public ICollection<ParkingLot> ParkingLots
        {
            get
            {
                if (_parkingLots == null)
                {
                    var newCollection = new FixupCollection<ParkingLot>();
                    newCollection.CollectionChanged += FixupParkingLots;
                    _parkingLots = newCollection;
                }
                return _parkingLots;
            }
            set
            {
                if (!ReferenceEquals(_parkingLots, value))
                {
                    var previousValue = _parkingLots as FixupCollection<ParkingLot>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupParkingLots;
                    }
                    _parkingLots = value;
                    var newValue = value as FixupCollection<ParkingLot>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupParkingLots;
                    }
                }
            }
        }
        private ICollection<ParkingLot> _parkingLots;

        #endregion

        #region Association Fixup

        private void FixupParkingLots(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (ParkingLot item in e.NewItems)
                {
                    if (!item.Partners.Contains(this))
                    {
                        item.Partners.Add(this);
                    }
                }
            }

            if (e.OldItems != null)
            {
                foreach (ParkingLot item in e.OldItems)
                {
                    if (item.Partners.Contains(this))
                    {
                        item.Partners.Remove(this);
                    }
                }
            }
        }

        #endregion

    }

我错过了什么吗?

4

1 回答 1

1

查看控制器代码,您没有将空模型传递给视图,因此标签没有任何内容可以绑定,因此出现空引用异常。我认为您需要以下内容。

//
        // GET: /Panel/Partners/Create/
        [Authorize]
        public ActionResult Create()
        {
            ViewData["Logo"] = "";
            var model = new MyModel()
            return View(model);
        }

编辑
我认为正是这行代码给您带来了问题 <input type="text" id="Name" name="Name" value="@Model.Name" class="form-control" /> ,因为它试图访问模型的 Name 属性,如果您不向视图传递任何内容,该属性为空。添加上面的代码为视图提供了一个模型来引用,因此可以检索 Name 属性的值

你是

于 2013-08-26T02:09:28.103 回答