Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件上载控件返回Null_C#_Asp.net Mvc - Fatal编程技术网

C# 文件上载控件返回Null

C# 文件上载控件返回Null,c#,asp.net-mvc,C#,Asp.net Mvc,每当我在上传文件后点击submit,它就会返回null,我已经使用了上传文件所需的所有必要条件,但仍然得到了问题,我在stack overflow中看到了所有答案,我使用了enctype,文件上传的名称也与我传递给controller的相同 public ActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToke

每当我在上传文件后点击submit,它就会返回null,我已经使用了上传文件所需的所有必要条件,但仍然得到了问题,我在stack overflow中看到了所有答案,我使用了enctype,文件上传的名称也与我传递给controller的相同

public ActionResult Create()
        {
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Officername,Designation,FileBeforeTour,FileAfterTour,FileBeforeTourName,FileAfterTourName")] FileDetails fileDetails)
        {
              if (ModelState.IsValid)
              {
                string uploadedfilename = Path.GetFileName(fileDetails.filebeforetourupload.FileName);
                if (!string.IsNullOrEmpty(uploadedfilename))
                {


                    db.FileUpload.Add(fileDetails);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
              }

            return View(fileDetails);
        }
@model online stationaryRegister.Models.FileDetails
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm(“创建”、“文件”、FormMethod.Post、新的{enctype=“multipart/form data”}))
{
@Html.AntiForgeryToken()
文件详细信息

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Officername,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Officername,new{htmlAttributes=new{@class=“formcontrol”}}) @Html.ValidationMessageFor(model=>model.Officername,“,new{@class=“text danger”}) @LabelFor(model=>model.Designation,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Designation,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Designation,“,new{@class=“text danger”}) @Label(“文件”,htmlAttributes:new{@class=“control Label col-md-2”}) @* @LabelFor(model=>model.FileAfterTour,htmlAttributes:new{@class=“controllabel col-md-2”}) *@ } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
好的,那么这就是你应该做的。与其将
HttpPostedFileBase
字段作为模型的一部分,不如将其从模型中删除并执行此操作

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Officername,Designation,FileBeforeTour,FileAfterTour,FileBeforeTourName,FileAfterTourName")] FileDetails fileDetails, HttpPostedFileBase myFile)
{

}

然后你就可以从那里操纵它。有时浏览器无法正确地在模型中包含文件。

更改控制器的方法,因为这样可以解决问题

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(FileDetails fileDetails)
    {
          if (ModelState.IsValid)
          {
            string uploadedfilename = 
                 Path.GetFileName(fileDetails.filebeforetourupload.FileName);
            if (!string.IsNullOrEmpty(uploadedfilename))
            {
                db.FileUpload.Add(fileDetails);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
          }

        return View(fileDetails);
    }

@sunnykumar,您的绑定中没有包含filebeforetourupload,您正在使用它来获取文件名。如果它对您有效,请投赞成票。快乐编码!!但是filebeforetour并没有直接映射到数据库,我只是使用它来获取filename@sunnykumar,但您正在使用filebeforetourupload从中获取文件名。对吗?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Officername,Designation,FileBeforeTour,FileAfterTour,FileBeforeTourName,FileAfterTourName")] FileDetails fileDetails, HttpPostedFileBase myFile)
{

}
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(FileDetails fileDetails)
    {
          if (ModelState.IsValid)
          {
            string uploadedfilename = 
                 Path.GetFileName(fileDetails.filebeforetourupload.FileName);
            if (!string.IsNullOrEmpty(uploadedfilename))
            {
                db.FileUpload.Add(fileDetails);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
          }

        return View(fileDetails);
    }