我无法获取文件输入值asp.net mvc Entity Framework [英] I can't get the value of my file input asp.net mvc Entity Framework

查看:66
本文介绍了我无法获取文件输入值asp.net mvc Entity Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图像上传到文件夹中,并且试图将图像路径放入数据库中.但是我无法在asp.net mvc中获取输入文件.如何解决此问题以及如何在Controller中调用输入文件?

I'm trying to upload my images in a folder and i'm trying to put the imagepath in my database. But I can't get my input file in asp.net mvc. How can I fix this and how can I call the input file in my Controller?

@model SecundaireSchool.Models.tblLeraren

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm(new { enctype= "multipart/form-data" }))
{
@Html.AntiForgeryToken()

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

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

    <div class="form-group">
        @Html.LabelFor(model => model.foto, "test" , htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.foto, "test" , new { type = "file", @class = "input-file", @name = "file" })
        </div>
    </div>

    <!-- <div class="form-group">
        @@Html.LabelFor(model => model.foto, htmlAttributes: new { @@class = "control-label col-md-2" })
        <div class="col-md-10">
            @@Html.EditorFor(model => model.foto, new { htmlAttributes = new { @@class = "form-control" } })
            @@Html.ValidationMessageFor(model => model.foto, "", 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>

TeacherController

所以我成功地将图像路径插入到我的数据库中.但它不会将其添加到我的图像文件夹中.我在做什么错了?

TeacherController

So i succeeded to insert the image path to my db. But it doesn't add it in my image folder. What am I doing wrong?

[HttpPost]
    public ActionResult Create(tblLeraren teacher, HttpPostedFileBase file = null)
    {
        if (ModelState.IsValid)
        {

            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                file.SaveAs(path);
                teacher.foto = path;
            }

            db.tblLerarens.Add(teacher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(teacher);
    }

推荐答案

在MVC中,文件对象作为 HttpPostedFileBase 的对象返回.因此,只需在发布表单的控制器中捕获该对象,它便会起作用.

In MVC the file object is returned as an object of HttpPostedFileBase. So just capture this object in the controller you post your form, and it will work.

[HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase file)
    {

    }

这篇关于我无法获取文件输入值asp.net mvc Entity Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆