[英]MVC 4 Razor Engine HttpPostedFileBase not working with Create a strongly-typed view
在我的MVC 4项目中,我使用“创建强类型视图”作为“脚手架模板:编辑”来创建视图。 但是在该页面中,我尝试使用文件上传方法上传图像,但是在控制器端,运行项目时,HttpPostedFileBase始终显示为null。
这是我的控制器:
public ActionResult EditUser(int id)
{
if (Session["UserId"] != null)
{
return View(db.Users.FirstOrDefault(x => x.id == id));
}
else
{
return RedirectToAction("Login");
}
}
[HttpPost]
public ActionResult EditUser(User user, HttpPostedFileBase ProfileImage)
{
db.Entry(user).State = System.Data.EntityState.Modified;
if (ProfileImage != null && ProfileImage.ContentLength > 0)
{
string filePath = Path.Combine(Server.MapPath("~/Content/img/user"), Path.GetFileName(ProfileImage.FileName));
ProfileImage.SaveAs(filePath);
}
db.SaveChanges();
return RedirectToAction("Profile");
}
这是我的观点:
@model emirhanozkan.Models.User
@{
ViewBag.Title = "Edit User";
}
<h2>EditUser</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>@ViewBag.Title</legend>
@Html.HiddenFor(model => model.id)
<div class="editor-label">
@Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Username)
@Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProfileImage)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.ProfileImage, new { type = "file", enctype = "multipart/form-data" })
@Html.ValidationMessageFor(model => model.ProfileImage)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
在输入文件系统中,将名称获取为ProfileImage。
这是我的模特:
namespace emirhanozkan.Models
{
public partial class User
{
[Key]
public int id { get; set; }
[Required(ErrorMessage = "Lütfen bir kullanıcı adı belirleyiniz.")]
public string Username { get; set; }
[Required(ErrorMessage = "Lütfen email adresinizi giriniz.")]
[RegularExpression(@"^([\w-\.]+)@((\[[0-9]{1,3]\.)|(([\w-]+\.)+))([a-zA-Z{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Lütfen geçerli bir email adresi giriniz.")]
public string Email { get; set; }
public string ProfileImage { get; set; }
}
}
@Html.TextBoxFor(model => model.ProfileImage, new { type = "file", enctype = "multipart/form-data" })
是我的选择,我不知道有没有这样的方法,但是我尝试过使用@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })){<input type="file" />}
代替我的代码,但是没有也可以。
您对在MVC 4上的“创建强类型视图”中如何使用HttpPostedFileBase有任何想法吗?
您必须在beginform中指定enctype multipart,例如
@using (Html.BeginForm("action", "controller", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
}
在这里,我们使用Html.BeginForm发送“ multipart / form-data ”,基本上,每当您向控制器发送文件时,都需要提及它:
该代码将为您工作:
@using (Html.BeginForm("EditUser", "controller", FormMethod.Post, new {enctype = "multipart/form-data" }))
{
}
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.