【问题标题】:razor add image with path not working asp.net剃须刀添加图像路径不起作用asp.net
【发布时间】:2017-08-25 12:46:55
【问题描述】:

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 中的问题 La référence d'objet n'est pas définie à une instance d'un objet。

描述 : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle。 Contrôlez la trace de la pour pour plus d'informations sur l'erreur et son origine dans le code。

details de l'exception: System.NullReferenceException: La référence d'objet n'est pas définie à une instance d'un object.

错误来源:

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 语言:56 Trace de la pile:

[NullReferenceException: La référence d'objet n'est pas définie à une instance d'un objet.] JobOfersWebSite.Controllers.jobsController.Create(job job, HttpPostedFileBase 上传) 在 c:\Users\DelManari\Documents\Visual Studio 2013\Projects\JobOfersWebSite\JobOfersWebSite\Controllers\jobsController.cs:56 lambda_method(闭包,ControllerBase,对象[])+147 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +157 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 参数) +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,控制器控制器)+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 结果) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9986301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 2020-07-15
      • 2012-04-09
      • 2013-02-22
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      相关资源
      最近更新 更多