剃刀添加图像路径无效 asp.net

razor add image with path not working asp.net

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 中的问题 对象引用未设置为对象的实例。

问题描述:当前网络请求执行过程中出现未处理的异常。检查堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

来源错误:

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;

源文件:c:\Users\DelManari\Documents\Visual Studio 2013\Projects\JobOfersWebSite\JobOfersWebSite\Controllers\jobsController.cs 行:56 堆栈跟踪:

[NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.] JobOfersWebSite.Controllers.jobsController.Create(job job, HttpPostedFileBase upload) in 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[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +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.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3f() +50 System.Web.Mvc.Async.<>c__DisplayClass48.b__41() +228 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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& completedSynchronously) +155

问题可能是由于打字错误 - @using(Html.BeginForm("Create", "jobs", FormMethod.Post, new {enctype="multipart/from-data" }))

应该是enctype="multipart/form-data"