Asp.net mvc 5 HttpPostedFileBase始终保持空mvc 5
我知道这个问题问了很多次,但没有一个答案对我有用。 我的观点是Asp.net mvc 5 HttpPostedFileBase始终保持空mvc 5,asp.net-mvc-5,httppostedfilebase,Asp.net Mvc 5,Httppostedfilebase,我知道这个问题问了很多次,但没有一个答案对我有用。 我的观点是 <div class="form-group"> @Html.LabelFor(model => model.VHF, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.VH
<div class="form-group">
@Html.LabelFor(model => model.VHF, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.VHF)
@Html.ValidationMessageFor(model => model.VHF)
</div>
</div>
@using (Html.BeginForm("Create", "Radio", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile" />
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
那么,我的错误是什么
谢谢老实说,这是我的第一个web应用程序。尝试一下,我的背景是WinForms应用程序。你的表单甚至没有提交按钮,只有一个表单控件(文件输入),因此不清楚你在这里做什么。最好的猜测是你的视图中有另一个
元素,它是无效的html。我正在尝试上载一张图片以及其他详细信息,如模型名,品牌等提交按钮在下一个div部分,我应该包括它与相同的形式部分?。。。谢谢你有另一张表格附在你展示的表格里吗?如果是这样,您需要删除它并将新的{enctype=“multipart/form data”}
添加到主表单中
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Model_No,Manufacturer_Id,Display,Channel_Capacity,Max_Freq_Separation,UHF_R1,UHF_R2,VHF,Pic")] sys_Radio_Models radioModels, HttpPostedFileBase uploadFile)
{
if (ModelState.IsValid)
{
if(uploadFile != null && uploadFile.ContentLength > 0)
{
//File size must be 2 MB as maximum
if (uploadFile.ContentLength > (2 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
return View();
}
//File types allowed : jpeg, jpg, png, gif and tif
if (!(uploadFile.ContentType == "image/jpeg"
|| uploadFile.ContentType == "image/jpg"
|| uploadFile.ContentType == "image/png"
|| uploadFile.ContentType == "image/gif"
|| uploadFile.ContentType == "image/tif"))
{
ModelState.AddModelError("CustomError", "File types allowed : jpeg, jpg, png, gif and tif");
return View();
}
byte[] data = new byte[uploadFile.ContentLength];
uploadFile.InputStream.Read(data, 0, uploadFile.ContentLength);
radioModels.Pic = data;
}
else
{
// Set the default image:
var img = Image.FromFile(Server.MapPath(Url.Content("~/assets/img/nopic.png")));
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
radioModels.Pic = new byte[ms.Length];
ms.Read(radioModels.Pic, 0, (int)ms.Length);
}
db.sys_Radio_Models.Add(radioModels);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Manufacturer_Id = new SelectList(db.sys_Manufacturers, "Manufacturer_Id", "Manufacturer_Name_a", radioModels.Manufacturer_Id);
return View(radioModels);
}