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

查看:21
本文介绍了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.TextBoxFor(x => x.Prop1)
    @Html.TextBoxFor(x => x.Prop2)
    @Html.TextBoxFor(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天全站免登陆