-2
我想我的MVC应用程序,我想在我看来做一个不错的文件上传。我的控制器是这样的,到目前为止:MVC文件上传
public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
{
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
model.groups = entities.Groups.ToList();
model.users = entities.Users.ToList();
if(ModelState.IsValid)
{
}
else
{
model.subject_id = model.subject_id;
model.groups = model.groups;
model.users = model.users;
return View(model);
}
return View(model);
}
在我看来,我想作一个文件上传,这样我可以在if(ModelState.IsValid
检索,然后上传它作为服务器的文件。我该怎么做呢?
编辑:
所以我说这个我的看法:
<div class="form-group">
@Html.LabelFor(m => m.file, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<input type="file" name="file" />
</div>
</div>
但我怎么通过选择文件,以在模型中定义的HttpPostedFileBase file
?
EDIT2: 我的观点,如果现在是这样的:
@model ClassDeclarationsThsesis.Models.PickGroupForHomeworkViewModel
@{
ViewBag.Title = "Pick Group For Homework";
}
<h2>Setting homework</h2>
@foreach (var user in Model.users)
{
if (user.email.Replace(" ", String.Empty) == HttpContext.Current.User.Identity.Name)
{
if (user.user_type.Replace(" ", String.Empty) == 2.ToString()|| user.user_type.Replace(" ", String.Empty) == 3.ToString())
{
using (Html.BeginForm("PickGroupForHomework", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<hr />
<div class="form-group">
@Html.LabelFor(m => m.deadline, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.deadline, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.file, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<div class="editor-field">
@Html.EditorFor(m=>m.file, new { @class="col-md-2 control-label"})
@Html.ValidationMessageFor(m=>m.file)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Submit" />
</div>
</div>
}
}
if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
{
<p>You do not have enough permissions to enter this page. Contact the administrator.</p>
}
}
}
'ModelState.IsValid'返回TRUE或'FALSE'。您无法从中检索文件。 – Shyju
当然我不能从这里检索它。但是如果一个模型是有效的,我可以从模型中检索它。 @Shyju –
[File Upload ASP.NET MVC 3.0]可能的重复(http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0) – Kamo