asp.net-mvc-3 - 无法向生成的 MVC3 页面添加文件上传功能

标签 asp.net-mvc-3

我是 MCV 新手,正在学习 MVC3。我创建了一个模型和一个 Controller ,并为我生成了 View 。生成的代码对我来说非常有意义。我想修改生成的 View 和 Controller ,以便在“创建”新记录时可以上传文件。有很多关于如何做到这一点的好信息。具体来说,我尝试了这个:http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

问题是,即使我选择一个文件(不大)并提交,请求中也没有文件。即Request.Files.Count为0。

如果我在同一个项目(无模型)中从头开始创建 Controller 和 View ,则该示例可以正常工作。我只是无法将该功能添加到生成的页面中。基本上,我正在尝试获取“创建”操作来发送文件。例如,创建一个新产品条目并随其发送图片。

创建 View 示例:

@model Product.Models.Find

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Create", "Find", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Find</legend>

        <input type="file" id="file" /> 


    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

示例 Controller :

    [HttpPost]
    public ActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            if (Request.Files.Count > 0 && Request.Files[0] != null)
            {
                //Not getting here
            }

            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(find);
    }

这将很好地创建记录,但没有与请求关联的文件。

我也尝试过这样的 Controller 操作:

    [HttpPost]
    public ActionResult Create(HttpPostedFileBase file)
    {
        if (file.ContentLength > 0)
        {
            //Not getting here
        }

        return RedirectToAction("Index");
    }

我想知道您是否无法在发布表单字段的同时发布文件?如果是这种情况,创建新记录并将图片(或其他文件)与其关联的模式是什么?

谢谢

最佳答案

创建一个 ViewModel,它具有处理图像和产品详细信息的属性

public class ProductViewModel
{
 public string ImageURL { set;get;}
 public string Title { set;get;}
 public string Description { set;get;}
}

并在您的 HTTPGET Action 方法中,将此 ViewModel 对象返回到您的强类型 View

 public ActionResult Create()
 {
        ProductViewModel objVM = new ProductViewModel();
        return View(objVM);
 }

在你看来

@model ProductViewModel 
<h2>Add Product</h2>
 @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  @Html.TextBoxFor(m => m.Title) <br/>
  @Html.TextBoxFor(m => m.Description ) <br/>
  <input type="file" name="file" />
  <input type="submit" value="Upload"  />
  @Html.HiddenFor(m => m.ImageURL )
}

现在在您的 HttpPost 操作方法中,接受此 ViewModel 和文件

[HttpPost]
public ActionResult Create(HttpPostedFileBase file, ProductViewModel objVM)
{
  if(file==null)
  {
     return View("Create",objVM);
  }
 else
 {
    //You can check ModeState.IsValid if you have to check any model validations and do further processing with the data here.
    //Now you have everything here in your parameters, you can access those and save
 }        
}

关于asp.net-mvc-3 - 无法向生成的 MVC3 页面添加文件上传功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067893/

相关文章:

c# - 带有部分页面的客户端的 Outputcache 属性

asp.net-mvc-3 - 将内联覆盖 CSS 移动到样式表中

ASP.NET MVC - 如何将子站点 (ASP.NET) 添加到我的应用程序?

c# - ASP.NET MVC/JqGrid : Is the JSON Id processed and recoverable?

c# - 如何将这些 LINQ 结果加载到我的 ViewModel 类中?

c# - 即使它是一个列表,也无法在处理后访问数据对象

asp.net-mvc-3 - MVC 3 with Razor 关于部分 View 的问题

asp.net-mvc-3 - ASP.NET MVC 3 中的元标记

asp.net-mvc-3 - 如何在 Orchard 的替代 View 中包含模块资源?

c# - 创建新模型时传递参数