我有一个控制器,其中我有Create方法。我的目标是上传图片以及特定人员所涉及的名称和服务。我的代码非常好。但是我遇到错误'对象引用未设置为控制器中的一个对象'在'行'if(leader.File.ContentLength>(2 * 1024 * 1024))'的实例。代码非常好。请让我知道我在哪里做错了
我有这样的模型类:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Required(ErrorMessage="The Name is required")]
[MaxLength(20,ErrorMessage="The Maximum characters allowed is 20")]
[MinLength(4,ErrorMessage="The Minimum charcaters allowed is 4")]
public string Name { get; set; }
[Required(ErrorMessage="Picture is required")]
public byte[] Picture { get; set; }
public int ImageSize { get; set; }
[NotMapped]
public HttpPostedFileBase File { get; set; }
public int MinistryID { get; set; }
[ForeignKey("MinistryID")]
public virtual Ministry Ministry { get; set; }
我有一个带有这样的create方法的Controller类:
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Leader leader)
{
if (leader.File.ContentLength > (2 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "File size must be
less than 2 MB");
return View();
}
if (!(leader.File.ContentType == "image/jpeg" ||
leader.File.ContentType == "image/gif"))
{
ModelState.AddModelError("CustomError", "File type allowed :
jpeg and gif");
return View();
}
leader.ImageSize = leader.File.ContentLength;
byte[] data = new byte[leader.File.ContentLength];
leader.File.InputStream.Read(data, 0,
leader.File.ContentLength);
leader.Picture = data;
if(ModelState.IsValid)
{
db.Leaders.Add(leader);
db.SaveChanges();
}
return View(leader);
}
}
Create方法的视图是:
@model ChurchWebsite.Models.Leader
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Leader</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div>
@Html.LabelFor(model=>model.Picture)
</div>
<div>
@using (Html.BeginForm("Upload","Image", FormMethod.Post,new{enctype="multipart/form-data"}))
{
@Html.TextBoxFor(Model=> Model.File, new{type="file",enctype="multipart/form-data"})
@Html.ValidationMessage("CustomError")
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MinistryID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MinistryID)
@Html.ValidationMessageFor(model => model.MinistryID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:1)
您有嵌套的表单,这些表单是无效的html,当您单击内部表单中的提交按钮时,它将发布没有enctype="multipart/form-data"
属性的外部表单,因此,文件不会被发送在控制器中,leader.File
为空,访问其ContentLength
属性会抛出异常。您需要删除内部嵌套表单元素并将enctype
属性添加到外部表单。
你的观点应该是(不相关的html删除)
@model ChurchWebsite.Models.Leader
....
@using (Html.BeginForm("Create", YourControllerName, FormMethod.Post, new{ enctype="multipart/form-data" })) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
....
@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
....
@Html.TextBoxFor(m => m.File, new{ type="file" }) // remove the inva;id enctype attribute
@Html.ValidationMessage("CustomError")
....
@Html.LabelFor(model => model.MinistryID)
@Html.EditorFor(model => model.MinistryID)
@Html.ValidationMessageFor(model => model.MinistryID)
<input type="submit" value="Create" />
}