Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
Asp.net mvc 将映像路径提交到数据库_Asp.net Mvc_Entity Framework_Visual Studio 2015 - Fatal编程技术网

Asp.net mvc 将映像路径提交到数据库

Asp.net mvc 将映像路径提交到数据库,asp.net-mvc,entity-framework,visual-studio-2015,Asp.net Mvc,Entity Framework,Visual Studio 2015,我想输入一些数据,然后将其保存到DB表中,其他字段在提交到数据库时工作正常。但在保存图像路径时,它会给出null。我在网上搜索,发现的东西都过时了,大部分都不管用 控制器 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Insert([Bind(Include ="id, ImageName, ImageSize")] ImageInfo info, HttpPostedFileBase ImagePat

我想输入一些数据,然后将其保存到DB表中,其他字段在提交到数据库时工作正常。但在保存图像路径时,它会给出null。我在网上搜索,发现的东西都过时了,大部分都不管用

控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Insert([Bind(Include ="id, ImageName, ImageSize")] ImageInfo info, HttpPostedFileBase ImagePath)
    {
        if(ModelState.IsValid)
        {
            if (ImagePath != null)
            {
                var filename = Path.GetFileName(ImagePath.FileName);
                var path = Path.Combine(Server.MapPath("~/Uploads"), filename);
                ImagePath.SaveAs(path); 

                ImagePath.SaveAs(HttpContext.Server.MapPath("~/Uploads") + ImagePath.FileName);
                info.ImagePath = ImagePath.FileName; 
            }
            db.ImageInfoes.Add(info);
            db.SaveChanges();
        return RedirectToAction("Index"); 
    }

        return View(info);
     }
看法

@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
形象

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.id,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.id,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.id,“,new{@class=“text danger”}) @LabelFor(model=>model.ImageName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ImageName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ImageName,“,new{@class=“text danger”}) @LabelFor(model=>model.ImageSize,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.ImageSize,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.ImageSize,“,new{@class=“text danger”}) @*@Html.TextBoxFor(model=>model.ImagePath,新的{type=“file”})*@ @Html.ValidationMessageFor(model=>model.ImagePath,“,new{@class=“text danger”})
分贝


任何帮助都很好!

您想将
enctype=“multipart/form data”
添加到表单标记中

我个人喜欢将HttpPostedFileBase参数名重命名为file以避免与ImageInfo.ImagePath混淆

看法 作用方法
好的,很抱歉,这是一天之后,但我更改了代码以匹配您的,它只保存了文件名并保留了路径“~/Uploads”。我进行调试,当我添加一个手表时,它会显示“表达式已计算且没有值”。知道吗?
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Image</legend>
    <div class="editor-label">
        <div class="form-horizontal">
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.id, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.id, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.id, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ImageName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ImageSize, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ImageSize, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ImageSize, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @*@Html.TextBoxFor(model => model.ImagePath, new { type = "file" })*@
                <input type="file" name="ImagePath" id="ImagePath" />
                    @Html.ValidationMessageFor(model => model.ImagePath, "", new { @class = "text-danger" })
            </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>
</fieldset>
@using (Html.BeginForm("Index","Start",FormMethod.Post,new {enctype="multipart/form-data"}))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Image</legend>
        <div class="editor-label">
            <div class="form-horizontal">
                <hr/>
                ...
                <div class="form-group">
                    @* Notice name and id are named as file *@
                    <input type="file" name="file" id="file"/>
                    @Html.ValidationMessageFor(model => model.ImagePath, "", new {@class = "text-danger"})
                </div>
                ...
            </div>
        </div>
    </fieldset>
}
public class ImageInfo
{
    public string Id { get; set; }
    public string ImageName { get; set; }
    public string ImageSize { get; set; }
    public string ImagePath { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "id, ImageName, ImageSize")] ImageInfo info, 
  HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        if (file != null)
        {
            file.SaveAs(Server.MapPath("~/Uploads") + file.FileName);
            info.ImagePath = file.FileName;
        }
        /*db.ImageInfoes.Add(info);
        db.SaveChanges();*/
        return RedirectToAction("Index");
    }

    return View(info);
}