0

I'm beginner For the MVC ,Im Developed the My project for Image Upload Script, I want to display my uploaded image in grid, and some one need to replace the Uploaded image to other Image,My code is not working correctly, please help me, Image Image Model

public  class MainGroup
  {
    [DataType(DataType.ImageUrl)]
      public string Image { get; set; }
}

View -Index

<table style="border: 1px solid black;" cellpadding="10">
    <tr>
       <th>
            @Html.DisplayNameFor(model => model.Image)
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>


            <td valign="top">
                <img src="@Url.Content("~/Content/Uploaded/" + item.MainGroupID + "/" + item.Image + "")" alt="@item.MainGroupName" width="100px" height="100px" />
            </td>
            <td valign="top">
                @Html.ActionLink("Edit", "Edit", new { id = item.MainGroupID }, new { @class = "createActor", title = "Edit Actor" })
                |
                @Html.ActionLink("Delete", "Delete", new { id = item.MainGroupID })
            </td>
        </tr>
    }
</table>

Create-Chtml

        @using (@Html.BeginForm("Save", "MainGroup"))
        {

<div class="editor-field">
        <input type="file" name="imageFile" id="imageFile" />
        @Html.ValidationMessageFor(model => model.Image)
    </div>
            <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;"><b>Status</b></div>
            <div class="col-md-6">
                @Html.CheckBoxFor(a => a.StatusId)
            </div>

            <div class="col-md-6"></div> <div class="col-md-6"></div>

            <div class="col-md-6"></div>
            <div class="col-md-6">   <input type="submit" id="btn" value="Save" class="btn btn-success" /> </div>
        }
    </div>

}

Controller

 public class MainGroupController : Controller
    {
        // GET: Masters/MainGroup

        public ActionResult Save(MainGroup maingroup)
        {
            if (ModelState.IsValid)
            {
                var objContext = new KnittingdbContext();
                maingroup.CreateId = 1;
                maingroup.StatusChangeDate = System.DateTime.Now;
                maingroup.CreatedDate = System.DateTime.Now;
                maingroup.EditId = 1;
                maingroup.EditDate = System.DateTime.Now;
                objContext.MainGroups.Add(maingroup);

                objContext.SaveChanges();

                TempData["Success"] = "Saved Sucessfully";

                return RedirectToAction("MainGroupIndex", new { A = "New" });
            }





            else
            {
                return PartialView("_MainGroupCreate", maingroup);
            }


        }
Codeone
  • 1,173
  • 2
  • 15
  • 40

1 Answers1

2

You can define file as a HttpFileBase property to hold file then in post method(Save) move file to "~/Content/Uploaded/". Then try. Also you can check directory manually if image file available

public  class MainGroup
{
  [DataType(DataType.ImageUrl)]
  public string Image { get; set; }

  public HttpFileBase ImageFile { get; set; }

 }

public class MainGroupController : Controller { // GET: Masters/MainGroup

    public ActionResult Save(MainGroup maingroup)
    {
        if (ModelState.IsValid)
        {
            // You can add other logic also
            if(maingroup.ImageFile!= null && maingroup.ImageFile.ContentLength > 0)
            {
               string filePath = "~/Content/Uploaded/";
               string imageUrl = System.IO.Path.Combine(filePath, maingroup.ImageFile.File.FileName);
               var objContext = new KnittingdbContext();
               maingroup.CreateId = 1;
               maingroup.Image = imageUrl;
               maingroup.ImageFile.File.SaveAs(imageUrl));
               maingroup.StatusChangeDate = System.DateTime.Now;
               maingroup.CreatedDate = System.DateTime.Now;
               maingroup.EditId = 1;
               maingroup.EditDate = System.DateTime.Now;
               objContext.MainGroups.Add(maingroup);
               objContext.SaveChanges();
              TempData["Success"] = "Saved Sucessfully";


             }      

            return RedirectToAction("MainGroupIndex", new { A = "New" });
        }
        else
        {
            return PartialView("_MainGroupCreate", maingroup);
        }


    }
unique
  • 5,442
  • 6
  • 23
  • 26