MVC 3文件的上传和模型绑定 [英] MVC 3 file upload and model binding

查看:88
本文介绍了MVC 3文件的上传和模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格上传的作品,但我想通过模型信息为我的数据库来保存文件,当然不同的名称。

I have a form upload that works but I would like to pass model information for my database to save the file with a different name of course.

下面是我的Razor视图:

Here is my Razor view:

@model CertispecWeb.Models.Container

@{
  ViewBag.Title = "AddDocuments";
}

<h2>AddDocuments</h2>

@Model.ContainerNo

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

下面是我的控制器:

[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
     if (file.ContentLength > 0)
     {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"),
                       containers.ContainerNo);
        file.SaveAs(path);
     }

     return RedirectToAction("Index");
}

的模型信息不通过向控制器传递。我已阅读,我可能需要更新模型,我会怎么做呢?

The model information is not passed through to the controller. I have read that I might need to update the model, how would I do this ?

推荐答案

您的形式不包含除文件以外的任何输入标签,这样在你的控制器动作,你不能指望得到任何东西比上传的文件(这是所有的被发送到服务器)。实现这一目标的方法之一将是包括含有模型的ID,这将允许您从您的数据存储您要发布帖子(使用此如果用户不应该修改模型控制器动作内检索一个隐藏的标签,但只是附加文件):

Your form doesn't contain any input tag other than the file so in your controller action you cannot expect to get anything else than the uploaded file (that's all that's being sent to the server). One way to achieve this would be to include a hidden tag containing the id of the model which will allow you to retrieve it from your datastore inside the controller action you are posting to (use this if the user is not supposed to modify the model but simply attach a file):

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(x => x.Id)
    <input type="file" name="file" id="file" />
    <input type="submit" value="submit" />
}

,然后在你的控制器动作:

and then in your controller action:

[HttpPost]
public ActionResult Uploadfile(int id, HttpPostedFileBase file)
{
    Containers containers = Repository.GetContainers(id);
    ...
}

在,如果你想允许用户修改这种模式,那么你将需要包括要被发送到服务器模型的每个字段的正确输入字段另一方面:

On the other hand if you wanted to allow the user to modify this model then you will need to include the proper input fields for each field of your model that you want to be sent to the server:

@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBxFor(x => x.Prop1)
    @Html.TextBxFor(x => x.Prop2)
    @Html.TextBxFor(x => x.Prop3)
    <input type="file" name="file" id="file" />
    <input type="submit" value="submit" />
}

和那么你将有默​​认的模型绑定重建从请求这一模式:

and then you will have the default model binder reconstruct this model from the request:

[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
    ...
}

这篇关于MVC 3文件的上传和模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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