【问题标题】:I am trying to display Image in grid view but i can't do that Only Image name is showing , please suggest me solution我正在尝试在网格视图中显示图像,但我不能这样做只有图像名称正在显示,请建议我解决方案
【发布时间】:2020-01-22 07:14:06
【问题描述】:
  1. 这是我的控制器

    [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();
                }
    
            }
    
  2. 添加数据的代码

    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();
                }
            }
    
  3. 以下是我的创建和索引视图,数据显示在索引页面上

    @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


【解决方案1】:

您不仅要存储图像名称,还要将同一图像的路径存储到数据库中。

更多详情请参考下面的 URL 示例:

https://www.aspsnippets.com/Articles/Display-Images-inside-WebGrid-in-ASPNet-MVC.aspx

【讨论】:

【解决方案2】:

在创建视图中添加 FormMethod.post 方法

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 2021-12-25
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多