3
嘿... 我对我的视图有上传控制。有没有办法将这个控件与模型数据相关联(比如LabelFor或者TextBoxFor)。我需要这个,因为在页面加载我丢失我的信息在文件上传控制 ThxMVC - 文件上传
嘿... 我对我的视图有上传控制。有没有办法将这个控件与模型数据相关联(比如LabelFor或者TextBoxFor)。我需要这个,因为在页面加载我丢失我的信息在文件上传控制 ThxMVC - 文件上传
是的,使用HttpPostedFileBase类的属性类型,它将绑定就像任何其他属性会。
HTML文件上传ASP MVC 3
型号:(注意FileExtensionsAttribute是MvcFutures可用它会验证文件扩展名的客户端和服务器端。)
public class ViewModel
{
[Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv", ErrorMessage = "Specify a CSV file. (Comma-separated values)")]
public HttpPostedFileBase File { get; set; }
}
HTML查看:
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.File, new { type = "file" })
@Html.ValidationMessageFor(m => m.File)
}
控制器动作:
[HttpPost]
public ActionResult Action(ViewModel model)
{
if (ModelState.IsValid)
{
// Use your file here
using (MemoryStream memoryStream = new MemoryStream())
{
model.File.InputStream.CopyTo(memoryStream);
}
}
}
我仍然有同样的问题,在uploadfile值时,页面重新加载 – Cipiripi 2011-01-11 14:56:42