-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