[英]Upload Image & Model ASP.NET MVC 4
Ok, I keep getting null returned on my photo, What am I missing? 好的,我的照片上一直没有返回null,我错过了什么?
I've trid this Uploading/Displaying Images in MVC 4 我在MVC 4中试图上传/显示图像
But Im obviously looking to include the image as part of model. 但我显然希望将图像作为模型的一部分。 Model 模型
public class Client
{
public string Name{ get; set; }
public string ClientId { get; set; }
public HttpPostedFileBase Photo{ get; set; }
}
Page 页
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div class="form-horizontal" role="form">
<div class="form-group">
<label for="@Model.Name" class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input name="ClientId" type="hidden" value="@Model.ClientId" id="ClientId" data-val="true" data-val-required=" required">
<input type="text" id="Name" value="" class="form-control" tabindex="1" placeholder="name" name="Name" required/>
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<label for="@Model.Name" class="col-sm-2 control-label">Image:</label>
<div class="col-sm-10">
@Html.TextBoxFor(m => m.Photo, new { type = "file" })
</div>
</div>
}
Controller 调节器
[HttpPost]
[Authorize]
public ActionResult New(ViewModels.Client client)
{
if (client.Photo!= null)
{
var photo= new byte[client.Photo.ContentLength];
client.Photo.InputStream.Read(photo, 0, client.Photo.ContentLength);
}
}
You haven't set the enctype
on the form (make sure you change Controller
below to be the name of your controller): 您尚未在表单上设置enctype
(请确保将下面的Controller
更改为Controller
的名称):
@using (Html.BeginForm("New", "Controller", FormMethod.Post,
new { enctype = "multipart/form-data" }))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.