对象引用未设置为对象的实例

时间:2016-06-10 21:40:21

标签: asp.net asp.net-mvc

我是asp.net mvc的新手,我找不到如何制作可选下拉列表的解决方案。

我的模特:

public class ParentCategory
{

    public int Id { get; set; }
    [Required]
    [Display(Name = "Parent Category Name")]
    public string PCatName { get; set; }
    [Required]
    [Display(Name = "UrlSeo")]
    public string UrlSeo { get; set; }
    [Required]
    [Display(Name = "Description")]
    public string PCatLogo { get; set; }

    public ICollection<ChildCategory> ChildCategories { get; set; }
    }

    public class AddParentCategoryViewModel
    {
        public int Id { get; set; }
        public string PCatName { get; set; }
        public string UrlSeo { get; set; }
        public string PCatLogo { get; set; }
    }

我的控制器

    [HttpGet]
    public ActionResult AddNewPCat()
    {
        List<int> numlist = new List<int>();
        int num = 0;
        var newid = num;
        AddParentCategoryViewModel model = new AddParentCategoryViewModel();
        model.Id = newid;
        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    [ValidateInput(false)]
    public ActionResult AddNewPCat([Bind(Include = "Id,PCatName,UrlSeo,PCatLogo")]AddParentCategoryViewModel model, HttpPostedFileBase file)
    { 
            var fileName = Path.GetFileName(file.FileName);          
            var path = Path.Combine(Server.MapPath("~/Image/PCatLogo/"), fileName);
            file.SaveAs(path);
            model.PCatLogo = file.FileName;

        var pcategory = new ParentCategory
        {
            Id = model.Id,
            PCatName = model.PCatName,
            UrlSeo = model.UrlSeo,
            PCatLogo = model.PCatLogo

        };

        _pageRepository.AddNewParentCategory(pcategory);
        return RedirectToAction("Index", "Page");
    }

我的存储库是:

    public void AddNewParentCategory(ParentCategory pcategory)
    {
        _context.ParentCategories.Add(pcategory);
        Save();
    }

观看:

@model bandymas.Models.PageViewModels.AddParentCategoryViewModel
@using bandymas.Controllers;
@{
ViewBag.Title = "Add New PCat";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Scripts
{
<script src="~/Scripts/view.js"></script>
<script src="~/ckeditor/ckeditor.js"></script>
}

@using (Html.BeginForm("AddNewPCat", "Page", null, FormMethod.Post, new { role = "form" }))
   {
   @Html.AntiForgeryToken()
    <div class="editPostContainer">
    <table>
        @*<tr>
                <td>Id :</td>
                <td colspan="2" class="editPageId">@Html.TextBoxFor(m => m.Id, new { @class = "editIdInp", @readonly = "readonly" })</td>
            </tr>*@
        <tr>
            <td>PCatName :</td>
            <td colspan="2" class="editPageUserName">@Html.TextBoxFor(m => m.PCatName, new { @class = "editPCatNameInp" })</td>
        </tr>
        <tr>
            <td>UrlSeo :</td>
            <td colspan="2">@Html.TextBoxFor(m => m.UrlSeo, new { @class = "editUrlSeo" })</td>
        </tr>
        <tr>
            <td>PCatLogo :</td>
            @*<td colspan="2">@Html.TextBoxFor(m => m.PCatLogo, new { @class = "editPCatLogo" })</td>*@
            <td>
                <input id="ImagePath" title="Upload a category image"
                       type="file" name="file" />
            </td>
        </tr>
        <tr>
        <td></td>
        <td colspan="3" class="editPageBody"><input class="comTextBtn" type="submit" value="&#x27a5;" /></td>
    </tr>
    </table>
   </div>
   }

文件上传有问题。问题是:

  

发生了'System.NullReferenceException'类型的异常   bandymas.dll但未在用户代码中处理附加信息:   对象引用未设置为对象的实例。

这是我第一次面对这个问题,我无法在任何地方找到解决方案......提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在表单中添加enctype="multipart/form-data"以发布

等文件
Html.BeginForm(
    action, controller, FormMethod.Post, new { enctype="multipart/form-data"})