Create.cshtml(添加图片部分)
<div class="form-group">
@Html.LabelFor(model => model.JobImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="upload" />
@Html.ValidationMessageFor(model => model.JobImage, "", new { @class = "text-danger" })
</div>
</div>
完整的Create.cshtml
@model JobOfersWebSite.Models.job
@{
ViewBag.Title = "Create";
Layout = "~/Views/shared/_MainLayout.cshtml";
}
<h2>Create</h2>
@using System.Web.Mvc;
@using(Html.BeginForm("Create", "jobs", FormMethod.Post, new {enctype="multipart/from-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>job</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.JobTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobDiscription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobDiscription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobDiscription, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JobImage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="upload" />
@Html.ValidationMessageFor(model => model.JobImage, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CategorieId, "Categorie", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategorieId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategorieId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
JobController.cshtml
public ActionResult Create( job job,HttpPostedFileBase upload)
{
if (ModelState.IsValid)
{
string path = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
upload.SaveAs(path);
job.JobImage = upload.FileName;
db.jobs.Add(job);
db.SaveChanges();
return RedirectToAction("Index");
}
FR中的问题 Laréférenced'objetn'estpasdéfinieàuneinstance d'un objet。
描述:Une例外非géréess'estproduite au moment de l'exécutiondelarequêteWebactuelle。 Contrôlezlatrace de la pile pour plus d'informations sur l'erreur et son origin dans le code。
Détailsdel'exception:System.NullReferenceException:Laréférenced'objetn'estpasdéfinieàuneinstance d'un objet。
Erreur来源:
Ligne 54 : if (ModelState.IsValid)
Ligne 55 : {
Ligne 56 : string path = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
Ligne 57 : upload.SaveAs(path);
Ligne 58 : job.JobImage = upload.FileName;
Fichier来源:c:\ Users \ DelManari \ Documents \ Visual Studio 2013 \ Projects \ JobOfersWebSite \ JobOfersWebSite \ Controllers \ jobsController.cs Ligne:56 Trace de la pile:
[NullReferenceException:Laréférenced'objetn'estpasdéfinieàuneinstance d'un objet。 JobOfersWebSite.Controllers.jobsController.Create(作业作业,HttpPostedFileBase上传)在c:\ Users \ DelManari \ Documents \ Visual Studio 2013 \ Projects \ JobOfersWebSite \ JobOfersWebSite \ Controllers \ jobsController.cs:56 lambda_method(Closure,ControllerBase,Object [])+147 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object []参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary
2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2个参数)+27 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+22 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3f()+50 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass48.b__41()+228 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass33.b__32(IAsyncResult asyncResult)+10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass2b.b__1c()+26 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21.b__1e(IAsyncResult asyncResult)+100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+27 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState)+13 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+36 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult,Controller controller)+12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult,ProcessRequestState innerState)+21 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9986301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
答案 0 :(得分:0)
问题可能是由于错字 - @using(Html.BeginForm(&#34; Create&#34;,&#34; jobs&#34;,FormMethod.Post,new {enctype =&#34; multipart / from-data & #34;}))
应该是enctype =&#34; multipart / form-data &#34;