-1

I have a model class like following

public class UserViewModel
{
    public UserViewModel() 
    {
        UsersDet = new Dictionary<long, string>(); 
        usrdat  = new List<User>();    
    }

    public Dictionary<long , string> UsersDet { get; set; }
    public IEnumerable<User> usrdata { get; set; }
}

Then I'm trying to get those model values in partial view like following

this partial view is a popup actually.

_UserView partial view

@model Project.ViewModels.UserViewModel

<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-body" style="width: 530px;">
            <div id="slider_view" style="display: block;">
                <div class="flexslider">
                    <ul class="slides">
                        @foreach (var item in Model.UsersDet )
                        {         
                            <li data-thumb="~/Upload_Images/A.jpg">
                                <img src="~/Upload_Images/A.jpg" data-imagedata="image001" />
                                <p class="flex-caption">Title</p>
                            </li>
                        }
                    </ul>
                </div>
            </div>
       </div>
    </div>
</div>

Here I cannot see the A.jpg images in Popup, but when I use like following I can see images

_UserView partial view

@model Project.ViewModels.UserViewModel

<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-body" style="width: 530px;">
            <div id="slider_view" style="display: block;">
                <div class="flexslider">
                    <ul class="slides">
                        @for (int i =0; i < 3 ; i ++)
                        {         
                            <li data-thumb="~/Upload_Images/A.jpg">
                                <img src="~/Upload_Images/A.jpg" data-imagedata="image001" />
                                <p class="flex-caption">Title</p>
                            </li>
                        }
                    </ul>
                </div>
            </div>
       </div>
    </div>
</div>

this is controller method to bind values to above popup

    [HttpGet]
    public PartialViewResult MethodName(int id)
    {
        try
        {    

            IEnumerable<User> listofData = ..
            UserViewModel listofimage = new UserViewModel();


            if (listofData != null) 
            {
                listofimage.usrdata = listofData;

                foreach(var item in listofimage.usrdata)
                {
                    var path = RenderImage(item.ImageValue, "UploadImagePath");                        

                    var fileResult = path as FilePathResult;

                    if (fileResult != null)
                    {

                        string imageurl = fileResult.FileName;

                        imageurl = imageurl.Replace(@"\\", @"\");

                        listofimage.UsersDet.Add(item.CaseID, imageurl);
                    }                        
                }
            }                

            return PartialView("_UserView", listofimages);   

        }
        catch (Exception)
        {
            throw;
        }

    } 

    public ActionResult RenderImage(string imageid, string pathvalue)
    {
        try
        {
            var URL = System.Configuration.ConfigurationManager.AppSettings[pathvalue].ToString();
            var path = Path.Combine(URL, imageid + ".jpg");
            return base.File(path, "image/jpeg");
        }
        catch (Exception)
        {
            throw;
        }
    }

Whats is wrong in my foreach approach how to populate elements as loop

kez
  • 2,273
  • 9
  • 64
  • 123
  • Are there any items in `UsersDet ` (where do you populate it)? –  Nov 17 '16 at 09:09
  • yeah multiple items may include – kez Nov 17 '16 at 09:12
  • @kez `Model.UsersDet` doesn't contains any item, debug your code or at least show number of items before foreach loop `@Model.UsersDet.Count` – Ahmed Nov 17 '16 at 09:23
  • @AhmedRagheb question updated , values getting actually when I debug I can see that, – kez Nov 17 '16 at 09:25
  • The only way that `@foreach (var item in Model.UsersDet )` could not be generating any html is if there are no items in the collection (and still using the nonsense from the last question I see :) –  Nov 17 '16 at 09:31
  • @kez Convert `IEnumerable listofData` = .. to `List` `.toList()` – Ahmed Nov 17 '16 at 09:37
  • @StephenMuecke do need to use to get images, for ex: `
  • `
  • – kez Nov 17 '16 at 10:15
  • Sorry, what does that have to do with the question? –  Nov 17 '16 at 10:17
  • to show images with foreach loop do I have to use like this `
  • ` other than this `
  • `
  • – kez Nov 17 '16 at 10:21
  • @StephenMuecke is that the reason image not showing when use `foreach` as you mention in this [question](http://stackoverflow.com/questions/40670382/get-viewpage-values-from-layout-page) as comment `(1). You cannot use foreach loops to generate form controls for a collection - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) (2). Duplicate id attributes are invalid - use class names and relative selectors` – kez Nov 18 '16 at 09:07
  • No. And what is your real problem - the code inside the `foreach` and `for` loops are identical - if your claiming the the `for` loops generated your html but the `foreach` loop does not then the is only one reason - you DO NOT have any items in the collection (or you have not shown the correct code and they are actually different in each loop) –  Nov 18 '16 at 09:12