簡體   English   中英

找到MVC控制器參數為空

[英]MVC controller parameter found null

當我從索引上傳文件時,Create controller參數可以找到null為什么? 我正在使用db模型是'File'和模型類是'MyfileModel'

  public ActionResult Create(HttpPostedFileBase filesBase)
    {         
         try
        {
            if (ModelState.IsValid)
            {
                db.Files.Add(new File()
                {
                    FileName = Path.GetFileName(filesBase.FileName)
                });
                db.SaveChanges();
            }
        }
        catch (Exception)
        {
            var error = "Sorry not save";
        }
        return Content("");
    }



@using (Html.BeginForm("Create", "Home", FormMethod.Post, new {enctype =    "multipart/form-data"}))
{
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload"/> 
}

您的輸入必須具有名稱“ fileBase”。 或將操作中輸入參數的名稱更改為“文件”。

看看我如何做到這一點,

控制器代碼:

    [HttpPost]
    public ActionResult File()
    {
        if (Request.Files.Count > 0)
        {                
            var file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                // do stuff
            }
        }
        return View();
    }

在查看文件中:

@using (Html.BeginForm("File", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

   <div class="form-group">
        <input name="file" type="file" />
    </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>
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM