ASP.NET MVC3上传从局部视图文件(并填写模型中相应的字段) [英] ASP.NET MVC3 Upload a file from a Partial view (and fill the corresponding field in the Model)

查看:93
本文介绍了ASP.NET MVC3上传从局部视图文件(并填写模型中相应的字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被<一个href=\"http://stackoverflow.com/questions/13788372/asp-net-mvc-ajax-file-upload-with-partialviews\">discussed在SO和其他地方,但我找不到任何回答我的问题。




我工作的一个ASP.NET MVC3的项目,我想创建一个局部视图的含文件上传。这部分观点被称为一个基本的创建页面上,我想上传的文件属于典型的创造。只有当用户将提交所选的文件将被上载的表格

下面是由code解释:



型号 ModelToCreate

 公共类ModelToCreate
{
    //一些性质    公共FileUploadModel文件{搞定;组; }
}



型号 FileUploadModel

 公共类FileUploadModel
{
    公共IEnumerable的&LT; HttpPostedFileBase&GT;文件{搞定;组; }
}



我PartialView(_UploadFiles.cshtml)

  @model Models.ModelToCreate//我试着用Html.BeginForm(创建,myController的,而不是NULL,NULL,但没有结果。
@using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
    @ Html.TextBoxFor(M = GT; m.Files,新的{type =文件,名字=文件})
}



局部视图是如何被调用(由Create.cshtml)

  @ Html.Partial(_ UploadFiles)

我也试图与 @ Html.Partial(_ UploadFiles模型),但没有结果...




当我点击 Create.cshtml 提交按钮,表单提交到我的控制器,但文件字段总是而其他的数据都是确定。




我缺少的东西吗?你能指出我在这里(为什么?)

谢谢!




UPDATE(与解决方案)

下面是一些其它附加信息,我忘了 Create.cshtml
外观形式是这样的:

  @using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ID =目标表}))
{
    //某些字段,文本框和其他的CheckBox    //调用局部视图
}

当我看看到生成的源$ C ​​$ C,我看到了一个&LT局部视图,表单&GT; 标记...所以我有一个&LT;标签&GT; &LT;标签&GT; ,这是非法的,忽略。这将导致问题

SOLUTION 就在这个标记添加到的BeginForm Create.cshtml

  @using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ID =目标表,ENCTYPE =的multipart / form-data的}))

和打电话给我偏查看

  @ Html.Partial(_ UploadFiles模型)


解决方案

好吧,这会得到它为你工作。

Create.cshtml 预览(形式和放大器;提交移动部分外)

  @using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{
   @ Html.Partial(_ UploadFiles模型)
   &LT;输入类型=提交值=提交/&GT;
}

_UploadFiles.cshtml 视图

  @model ModelToCreate@ Html.TextBoxFor(M = GT; m.Files.Files,新的{type =文件,名字=文件})

模式(改为清单,并注意在 FileUploadModel 构造函数初始化)。

 公共类ModelToCreate
{
    //一些性质
    公共FileUploadModel文件{搞定;组; }
}公共类FileUploadModel
{
   公共FileUploadModel()
   {
        文件=新的List&LT; HttpPostedFileBase&GT;();
   }   公开名单&LT; HttpPostedFileBase&GT;文件{搞定;组; }
}

控制器操作:

 公众的ActionResult的Create()
{
    VAR模型=新ModelToCreate();    返回查看(模型);}[HttpPost]
公众的ActionResult创建(ModelToCreate模型)
{
   var文件= model.Files.Files [0];
   返回查看(模型);
}

I know the subject has already been discussed on SO and elsewhere, but I can't find any answer to my question.

I'm working on an ASP.NET MVC3 project, and I'd like to create a Partial view containing a FileUpload. This partial view is called on a basic Create page, and I'd like the file to upload belongs to the model to create. It is only when the user will submit the form the selected file will be uploaded.

Here is an explanation by the code :


Model ModelToCreate

public class ModelToCreate
{
    //Some properties

    public FileUploadModel Files { get; set; }
}


Model FileUploadModel

public class FileUploadModel
{
    public IEnumerable<HttpPostedFileBase> Files { get; set; }
}


My PartialView (_UploadFiles.cshtml)

@model Models.ModelToCreate

//I tried with Html.BeginForm(Create, MyController instead of null, null, but with no result.
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })
}


How the partial view is called (by Create.cshtml)

@Html.Partial("_UploadFiles")

I also tried with @Html.Partial("_UploadFiles", Model), but with no result...

When I click the Submit button in Create.cshtml, the form is submitted to my controller BUT Files field is always null whereas the other datas are OK.

Am I missing something ? Could you indicate my where (and why ?)
Thank you !



UPDATE (and Solution)

Here is some additionnal information I forgot about Create.cshtml The form look like this :

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "target-form" }))
{
    // Some fields, Textboxes and others CheckBoxes

    //Call to partial View
}

When I take a look to generated source code, I see the partial view in a <form> tag... So I have a <tag> in a <tag>, which is illegal and "ignored". This causes the problem

SOLUTION Just add this tag to the BeginForm of Create.cshtml :

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "target-form", enctype = "multipart/form-data" }))

and call my Partial View as

@Html.Partial("_UploadFiles", Model)

解决方案

Ok, this will get it working for you.

Create.cshtml View (With the form & submit moved outside of the partial)

@using(Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   @Html.Partial("_UploadFiles", Model)
   <input type="submit" value="submit" />
}

_UploadFiles.cshtml view

@model ModelToCreate

@Html.TextBoxFor(m => m.Files.Files, new { type = "file", name = "Files" })

Models (Changed to a List and also note the initializer in the FileUploadModel constructor).

public class ModelToCreate
{
    //Some properties
    public FileUploadModel Files { get; set; }
}

public class FileUploadModel
{
   public FileUploadModel()
   {
        Files = new List<HttpPostedFileBase>();
   }

   public List<HttpPostedFileBase> Files { get; set; }
}

Controller actions:

public ActionResult Create()
{
    var model = new ModelToCreate();

    return View(model);

}

[HttpPost]
public ActionResult Create(ModelToCreate model)
{
   var file = model.Files.Files[0];
   return View(model);
}

这篇关于ASP.NET MVC3上传从局部视图文件(并填写模型中相应的字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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