0

I cannot add image in about -> post method. This is my code please help.

This is my controller section:

ActionResult Register(Admin adm)
{
    string fileName = Path.GetFileNameWithoutExtension(adm.ImageFile.FileName);
    string exe = Path.GetExtension(adm.ImageFile.FileName);
    fileName = fileName + DateTime.Now.ToString("yymmssfff") + exe;[enter image description here][1]
    adm.Adm_Image_path = "~/Image/" + fileName;
    fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
    adm.ImageFile.SaveAs(fileName);

    // Upload_Image(adm.ImageFile);

    if (ModelState.IsValid)
    {
        if (adm.Adm_Password == adm.Adm_Confirm_Password)
        {
            adm.Adm_Type = "Admin";
            db.admin.Add(adm);
            db.SaveChanges();

            return RedirectToAction("Login", "Home");
        }
    }

    return View();
}

View section here

<div class="form-group">
        @Html.LabelFor(x => x.Adm_Image_path, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="ImageFile" id="ImageFile" value="ImageFile" required />
            @Html.ValidationMessageFor(x => x.Adm_Image_path, "", new { @class="text-danger"})
        </div>
    </div>
    <input type="submit" value="Save" class="btn btn-default" />`

I cannot upload the image in the specific section.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Use Below code.

Model Class:

public class Admin
{
    public string ImagePath { get; set; }

    public HttpPostedFileBase ImageFile { get; set; }
}

Controller:

public ActionResult Register()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Register(Admin adm)
    {
        string fileName = Path.GetFileNameWithoutExtension(adm.ImageFile.FileName);
        string exe = Path.GetExtension(adm.ImageFile.FileName);
        fileName = fileName + DateTime.Now.ToString("yymmssfff") + exe;
        adm.ImagePath = "~/Images/" + fileName;
        fileName = Path.Combine(Server.MapPath("~/Images/"), fileName);
        adm.ImageFile.SaveAs(fileName);
        //Upload_Image(adm.ImageFile);

        if (ModelState.IsValid)
        {
            //if (adm.Adm_Password == adm.Adm_Confirm_Password)
            //{
            //    adm.Adm_Type = "Admin";
            //    db.admin.Add(adm);
            //    db.SaveChanges();
            //    return RedirectToAction("Login", "Home");
            //}
            return View();
        }
        else
        {
            return View();
        }
    }

View:

@model testProject.Models.Admin
 @{
      ViewBag.Title = "Register";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }

    <h2>Register</h2>
    @using (Html.BeginForm("Register", "Signup", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
      <div class="form-group">
        @Html.LabelFor(x => x.ImagePath, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
          <input type="file" name="ImageFile" id="ImageFile" value="ImageFile" required />
          @Html.ValidationMessageFor(x => x.ImagePath, "", new { @class = "text-danger" })
        </div>
      </div>
      <input type="submit" value="Save" class="btn btn-default" />
    }

Hope this will surely help you.

Niraj
  • 89
  • 1
  • 5