我在我的视图中上传图片的情况,然后应该填充模型中我的Components实体中的UploadedFile属性。但是,由于某种原因,从未达到过该物业。如果我输入一个名字,那就没问题了。我在这里错过了一些细节吗?
public partial class Components
{
public string Name { get; set; } // This gets set
public HttpPostedFileBase UploadedFile { get; set; } // This doesn't gets set
public byte[] Image { get { return ConvertToByte(UploadedFile); } set { ; } }
public byte[] ConvertToByte(HttpPostedFileBase uploadedFile)
{
MemoryStream ms = new MemoryStream();
uploadedFile.InputStream.CopyTo(ms);
byte[] imageBytes = ms.ToArray();
return imageBytes;
}
}
使用文件上传查看:
@model EmbeddedStock.Models.ComponentViewModel
div class="form-group">
<div class="col-md-10">
@Html.LabelFor(model => model.ComponentModel.Image, htmlAttributes: new {@class = "control-label col-md-2"})
<a class="btn" href="javascript:;">
Choose File...
<input type="file" name="UploadedFile" Size="40" style="position: absolute; z-index: 2; top: 0; left: 0; filter: alpha(opacity=0); opacity: 0; background-color: transparent; color: transparent"
onchange='$("#upload-file-info").html($(this).val());'/>
</a>
<span class="label label-info" id="upload-file-info"></span>
</div>
</div>
ComponentViewModel:
public class ComponentViewModel
{
public Components ComponentModel { get; set; }
}
答案 0 :(得分:1)
根据Stephen Muecke的建议,更改名称&#39;在视图的输入字段中完成了这项工作。
<div class="form-group">
<div class="col-md-10">
@Html.LabelFor(model => model.ComponentModel.Image, htmlAttributes: new {@class = "control-label col-md-2"})
<a class="btn" href="javascript:;">
Choose File...
<input type="file" name="ComponentModel.UploadedFile" Size="40" style="position: absolute; z-index: 2; top: 0; left: 0; filter: alpha(opacity=0); opacity: 0; background-color: transparent; color: transparent"
onchange='$("#upload-file-info").html($(this).val());'/>
</a>
<span class="label label-info" id="upload-file-info"></span>
</div>
</div>