我正在使用 mvc4 和实体框架 5 编写一个非常小的应用程序。
我想添加产品以及该产品的商店和图像。
我有一个模型
[Table("CatalogItem")]
public class CatalogItemModel
{
[Key]
public int CatalogItemId { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public int ProductCount { get; set; }
public string Size { get; set; }
public string Sku { get; set; }
[Column(TypeName = "image")]
public byte[] Image { get; set; }
[Display(Name = "Display Catalog Item")]
public bool DisplayItem { get; set; }
}
我的控制器。这永远不会被击中。
[HttpPost]
public ActionResult Create(CatalogItemModel catalogitemmodel)
{
if (ModelState.IsValid)
{
db.CatalogItemModels.Add(catalogitemmodel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(catalogitemmodel);
}
我的意见表
<fieldset>
<legend>CatalogItemModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProductCount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProductCount)
@Html.ValidationMessageFor(model => model.ProductCount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Size)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Size)
@Html.ValidationMessageFor(model => model.Size)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Sku)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Sku)
@Html.ValidationMessageFor(model => model.Sku)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DisplayItem)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DisplayItem)
@Html.ValidationMessageFor(model => model.DisplayItem)
</div>
<div class="editor-label">
@Html.LabelFor(m=>m.Image)
</div>
<input name="Image" type="file"/>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
当我尝试在文件输入中发布带有图像的新目录时,它会抛出错误
输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中包含非法字符。
尝试这样修复:
1.代替
<input name="Image" type="file"/>
with <input name="ImageFile" type="file"/>
2.在控制器中:
[HttpPost]
public ActionResult Create(CatalogItemModel catalogitemmodel, HttpPostedFileBase ImageFile)
{
using (var ms = new MemoryStream())
{
ImageFile.InputStream.CopyTo(ms);
catalogitemmodel.Image = ms.ToArray();
}
if (ModelState.IsValid)
{
db.CatalogItemModels.Add(catalogitemmodel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(catalogitemmodel);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)