我有一个来自SQL Server 2016的数据库模型,在编写文件类型为HttpPostedFileBase
时,我找不到我的viewmodel,那我该怎么办?
这是我的模型,视图和控制器
模型
public partial class HMW_Solution
{
public int ID { get; set; }
public Nullable<int> HMW_ID { get; set; }
public Nullable<int> Student_ID { get; set; }
public string Answer { get; set; }
public byte[] Sol_File { get; set; }
public virtual HMW HMW { get; set; }
public virtual Student Student { get; set; }
}
查看
<h2>AddAnswer</h2>
<div class="form-horizontal">
<h4>HMW_Solution</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<input type="hidden" name="HMW_ID" value=@ViewBag.HMW_ID>
<input type="hidden" name="Student_ID" value=@System.Web.HttpContext.Current.Session["StudentID"]>
<div class="form-group">
@Html.LabelFor(model => model.Answer, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Answer, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Answer, "", new { @class = "text-danger" })
</div>
</div>
@{
Byte[] file;
}
<input type="file" name="Sol_File" />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
控制器
[HttpPost]
public ActionResult AddAnswer([Bind(Include = "HMW_ID,Student_ID,Answer,Sol_File")]HMW_Solution s, HttpPostedFileBase Sol_File)
{
//byte[] bytes = s.Sol_File.ToArray();
//HttpPostedFileBase file = s.Sol_File;
//byte[] uploadedFile = new byte[s.Sol_File.Length];
//s.Sol_File.Read(uploadedFile, 0, uploadedFile.Length);
//using (Stream inputStream = Sol_File.InputStream)
//{
// MemoryStream memoryStream = inputStream as MemoryStream;
// if (memoryStream == null)
// {
// memoryStream = new MemoryStream();
// inputStream.CopyTo(memoryStream);
// }
// s.Sol_File = memoryStream.ToArray();
//}
if (Sol_File.ContentLength > 0)
{
var fileName = Path.GetFileName(Sol_File.FileName);
var guid = Guid.NewGuid().ToString();
var path = Path.Combine(Server.MapPath("~/uploads"), guid + fileName);
Sol_File.SaveAs(path);
string fl = path.Substring(path.LastIndexOf("\\"));
string[] split = fl.Split('\\');
string newpath = split[1];
string imagepath = "~/uploads/" + newpath;
upload.length = imagepath;
db.FileUploads.Add(upload);
db.SaveChanges();
}
db.HMW_Solution.Add(s);
db.SaveChanges();
return RedirectToAction("showHMW", "HMW", new { Cource_ID = System.Web.HttpContext.Current.Session["CourseID"] });
}