在我的控制器中,因为我希望能够填写有关视频的一些细节并实际上传它,所以Video类不需要实际视频,因为它将被传递给另一个Web服务。 / p>
public class VideoUploadModel
{
public HttpPostedFileBase vid { get; set; }
public Video videoModel { get; set; }
}
//
// POST: /Video/Create
[HttpPost]
public ActionResult Create(VideoUploadModel VM)
{
if (ModelState.IsValid)
{
db.Videos.AddObject(VM.videoModel);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
在我看来我有
@model LifeHighlightsShoeLace.Controllers.VideoController.VideoUploadModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Create", "Video", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Video</legend>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.KalturaID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.KalturaID)
@Html.ValidationMessageFor(model => model.videoModel.KalturaID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.Size)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.Size)
@Html.ValidationMessageFor(model => model.videoModel.Size)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.Date)
@Html.ValidationMessageFor(model => model.videoModel.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.UploadedBy)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.videoModel.UploadedBy)
@Html.ValidationMessageFor(model => model.videoModel.UploadedBy)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.videoModel.UserId, "User")
</div>
<div class="editor-field">
@Html.DropDownList("UserId", String.Empty)
@Html.ValidationMessageFor(model => model.videoModel.UserId)
</div>
<div class="editor-field">
<input name="model.vid" type="file" />
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
当我提交表单时,VM的videoModel部分已填写但vid实际文件为null。 有什么想法吗?
答案 0 :(得分:19)
根据OP评论更新
在web.config文件中设置Max文件长度 改变“?”您希望成为最大文件的文件大小,例如65536是64MB
<configuration>
<system.web>
<httpRuntime maxRequestLength="?" />
</system.web>
</configuration>
您无法将文件添加到模型中,它将在其自己的字段中而不是模型的一部分
<input name="videoUpload" type="file" />
您的行为不正确。它需要接受文件作为它自己的参数(或者如果多次使用IEnumerable<HttpPostedFileBase>
作为参数类型)
[HttpPost]
public ActionResult Create(VideoUploadModel VM, HttpPostedFileBase videoUpload)
{
if (ModelState.IsValid)
{
if(videoUpload != null) { // save the file
var serverPath = server.MapPath("~/files/" + newName);
videoUpload.SaveAs(serverPath);
}
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
如果您允许选择多个文件,则必须允许
[HttpPost]
public ActionResult Create(VideoUploadModel VM, IEnumerable<HttpPostedFileBase> videoUpload)
{
if (ModelState.IsValid)
{
if(videoUpload != null) { // save the file
foreach(var file in videoUpload) {
var serverPath = server.MapPath("~/files/" + file.Name);
file.SaveAs(serverPath);
}
}
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
答案 1 :(得分:2)
它没有绑定的原因是因为模型绑定器在绑定像你这样的复杂模型时只查看QueryString
,Form
和RouteData
。解决这个问题的方法是在action方法中使用另一个参数。 (将你的“名字”改为“vid”)
[HttpPost]
public ActionResult Create(VideoUploadModel VM, HttpPostedFileBase vid)
{
//add your vid to the model or whatever you want to do with it :)
if (ModelState.IsValid)
{
db.Videos.AddObject(VM.videoModel);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId);
return View(VM);
}
答案 2 :(得分:2)
适合我:
public class CreateVeiwModel
{
public Speaker Speaker { get; set; }
public Guid Guid { get; set; }
public HttpPostedFileBase File { get; set; }
}
控制器:
[HttpPost]
public ActionResult Create(CreateVeiwModel model)
{
if (ModelState.IsValid)
try
{
Repository.AddSpeaker(model.Speaker);
...
}
查看:
@Html.Label("Most Up-to-Date CV")
<input type="file" name="File"/>
我认为解决方案放在那里:Model.File
〜<input name="File"/>
答案 3 :(得分:0)
更改
<input name="model.vid" type="file" />
到
@Html.TextBoxFor(model => model.vid, new {type="file"})
取决于您页面上的其他内容以及呈现视图的位置,MVC将生成唯一ID,我认为您的硬编码ID与表单字段没有正确关联。