【发布时间】:2020-01-22 07:14:06
【问题描述】:
-
这是我的控制器
[HttpPost] public ActionResult Create([Bind] Employee emp, HttpPostedFileBase Photo) { try { if (Photo.ContentLength > 0) { string _ImageName = Path.GetFileName(Photo.FileName); string _path = Path.Combine(Server.MapPath("~/Upload/" + _ImageName), _ImageName); Photo.SaveAs(Server.MapPath(_path)); objemployee.AddEmployee(emp); ViewBag.Message = "Image Uploaded Successfully!!"; return View(); } else if (ModelState.IsValid) { objemployee.AddEmployee(emp); return RedirectToAction("Index"); } return View(emp); } catch { objemployee.AddEmployee(emp); //return Json(new { msg = "1" }, JsonRequestBehavior.AllowGet); return View(); } } -
添加数据的代码
public void AddEmployee(Employee emp) { using (SqlConnection con = new SqlConnection(ConnectionString)) { SqlCommand cmd = new SqlCommand("spaddEmployee", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@FirstName", emp.FirstName); cmd.Parameters.AddWithValue("@LastName", emp.LastName); cmd.Parameters.AddWithValue("@Gender", emp.Gender); cmd.Parameters.AddWithValue("@DOB", Convert.ToDateTime(emp.DOB)); cmd.Parameters.AddWithValue("@Hobby", emp.Hobby); cmd.Parameters.AddWithValue("@Photo", emp.Photo); cmd.Parameters.AddWithValue("@City", emp.City); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } -
以下是我的创建和索引视图,数据显示在索引页面上
@Html.LabelFor(model => model.Photo, htmlAttributes: new { @class= "control-label col-md-2" }) @Html.TextBox("Photo", "", new { type = "file" })
@Html.ValidationMessageFor(model => model.Photo, "", new { @class= "text-danger" })
【问题讨论】:
-
你没有使用任何
img标签来展示你的图片 -
无法工作
标签: html asp.net-mvc model-view-controller