locked
display the multiple images RRS feed

  • Question

  • User-1892044535 posted

    hi guys I need just to display the multiple images I insert it save will in path, but when he showed it does show up. I don't know where is my problem but this is my code please guys help me?

    My Controller:

    public async Task<ActionResult> Create(Inbox model, IEnumerable<HttpPostedFileBase> files)
    {
        var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
        if (ModelState.IsValid)
        {
            model.User = currentUser;
            foreach (var file in files)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
                    file.SaveAs(path);
                    path = Url.Content(Path.Combine("~/FilesAPP", fileName));
                }
            }
            db.Inboxs.Add(model);
            db.SaveChanges();
            string url = Url.Action("List");
            return Json(new { success = true, url = url });
        }
        return View(model);
    }

    My model:

    public string Files { get; set; }

    And my View

    @foreach (var item in Model.Inbox)
    {
        <img src="@item.Files" />
    }
    How can I display my images guys? I just want to display my images pleas
    Sunday, November 4, 2018 11:54 AM

All replies

  • User-1892044535 posted

    come on guys?

    Sunday, November 4, 2018 8:28 PM
  • User-474980206 posted

    you code makes little sense.

    in Create if the post back mode is invalid it return an html view, but if valid returns json???

    you save the post back model to the database, but never update it with paths to the saved images. how does it know?

    You state that your model has a string property of Files, but your sample loops thru a collection called in Inbox which has a proper of Files. How is this created?

    Sunday, November 4, 2018 11:06 PM
  • User1520731567 posted

    Hi mas mas,

    If you would like to display the images,you should have a field which in model to store the path after file.SaveAs(path).

    For example:

     var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
     file.SaveAs(path);
    
    
     Model md= new Model();
     md.imgpath = "~/FilesAPP/" + fileName;
    ...

    And then in View:

    <img src="@Url.Content(Model.imgPath)" />

    Best Regards.

    Yuki Tao

    Monday, November 5, 2018 8:37 AM
  • User-271186128 posted

    Hi mas,

    mas mas

    public async Task<ActionResult> Create(Inbox model, IEnumerable<HttpPostedFileBase> files)
    {
        var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
        if (ModelState.IsValid)
        {
            model.User = currentUser;
            foreach (var file in files)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
                    file.SaveAs(path);
                    path = Url.Content(Path.Combine("~/FilesAPP", fileName));
                }
            }
            db.Inboxs.Add(model);
            db.SaveChanges();
            string url = Url.Action("List");
            return Json(new { success = true, url = url });
        }
        return View(model);
    }
    @foreach (var item in Model.Inbox)
    {
        <img src="@item.Files" />
    }

    Please check your code, if you want to use the above code to display the image (which stored in the FilesAPP folder), the Files property should be used to store the image path. But, in the create method, you didn't assign the image path to the Files property. So, the image doesn't display. Try to assign the image path to the Files property, code as below:

            model.User = currentUser;
            foreach (var file in files)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
                    file.SaveAs(path);
                    path = Url.Content(Path.Combine("~/FilesAPP", fileName));
    
                    model.Files = path;
                }
            }       
            db.Inboxs.Add(model);
            db.SaveChanges();

    In the image List action, you could set a break point to check whether the File property contains the image path.

    Best regards,
    Dillion

    Thursday, November 15, 2018 6:54 AM