locked
For each loop in MVC controller action RRS feed

  • Question

  • User-501297529 posted

    I'm looking for assistance in create a for each loop in a MVC controller

    This is what I have so far, not sure what should go in the foreach statement

       public IActionResult Index()
        {
            var user = _userService.GetUsers();
            var viewModel = _mapper.Map<UserViewModel>(user);
            foreach (var user)
            {
                user.Username();
                user.Enabled();
            };
            return View(viewModel);
        }

    I expect this to loop thru each record to display users.

    Tuesday, July 16, 2019 1:11 PM

Answers

  • User475983607 posted

    bootzilla

    I expect this to loop thru each record to display users.

    The View is responsible for the UI.  A controller action is responsible for getting data for the View.  Does this line of code populate the viewModel variable?

    var viewModel = _mapper.Map<UserViewModel>(user);

    This block of code servers no purpose in the action.

            foreach (var user)
            {
                user.Username();
                user.Enabled();
            };

    Move the the foreach to your View which might look somehting like this...

    <table>
    	<thead>
    		<tr>
    			<th>
    				@Html.DisplayNameFor(model => model[0].Username)
    			</th>
    			<th>
    				@Html.DisplayNameFor(model => model[0].Enabled)
    			</th>
    			<th></th>
    		</tr>
    	</thead>
    	<tbody>
    		@foreach (var item in Model)
    		{
    			<tr>
    				<td>
    					@Html.DisplayFor(modelItem => item.Username)
    				</td>
    				<td>
    					@Html.DisplayFor(modelItem => item.Enabled)
    				</td>
    			</tr>
    		}
    	</tbody>
    </table>

    ... it depends on how your viewModel is designed which is not included above.

    I strongly recommend that you take a step back and learn the basics before moving forward.  Your code indicates that you do not understand the problem the MVC pattern solves.  If you do not understand the pattern then you will not be able to build an MVC application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 16, 2019 1:29 PM
  • User711641945 posted

    Hi bootzilla,

    According to your request, do you want to get the data of user but only map two properties of Username and Enabled?

    There are two ways to get the ViewModel:

    1.Use foreach loop :

    • UserViewModel.cs :
     public class UserViewModel
        {
            public string Username { get; set; }
            public bool Enabled { get; set; }
        }
    • Action :
    public IActionResult Index()
            {
                var user = _userService.GetUsers();
                var viewModel = new List<UserViewModel>();
                foreach (var users in user)
                {
                    viewModel.Add(new UserViewModel { Username = users.Username,Enabled=users.Enabled });
                };
                return View(viewModel);
            }

    2.Use AutoMapper :

    public IActionResult Index()
            {
                var user = _userService.GetUsers();
                var viewModel = _mapper.Map<List<UserViewModel>>(user);
                return View(viewModel);
            }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 6:56 AM

All replies

  • User753101303 posted

    Hi,

    The purpose of the loop is just to "display" users ? This is not the purpose of a controller. The controller will just transmit data to the view which then renders data using for example something such as :

    <table>
    @{
      foreach (var user in Model)
      {
         <tr>
           <td>@user.UserName</td>
           <td>@user.IsEnabled</td>
         </tr>
      }
    }
    </table>
    

    Tuesday, July 16, 2019 1:23 PM
  • User475983607 posted

    bootzilla

    I expect this to loop thru each record to display users.

    The View is responsible for the UI.  A controller action is responsible for getting data for the View.  Does this line of code populate the viewModel variable?

    var viewModel = _mapper.Map<UserViewModel>(user);

    This block of code servers no purpose in the action.

            foreach (var user)
            {
                user.Username();
                user.Enabled();
            };

    Move the the foreach to your View which might look somehting like this...

    <table>
    	<thead>
    		<tr>
    			<th>
    				@Html.DisplayNameFor(model => model[0].Username)
    			</th>
    			<th>
    				@Html.DisplayNameFor(model => model[0].Enabled)
    			</th>
    			<th></th>
    		</tr>
    	</thead>
    	<tbody>
    		@foreach (var item in Model)
    		{
    			<tr>
    				<td>
    					@Html.DisplayFor(modelItem => item.Username)
    				</td>
    				<td>
    					@Html.DisplayFor(modelItem => item.Enabled)
    				</td>
    			</tr>
    		}
    	</tbody>
    </table>

    ... it depends on how your viewModel is designed which is not included above.

    I strongly recommend that you take a step back and learn the basics before moving forward.  Your code indicates that you do not understand the problem the MVC pattern solves.  If you do not understand the pattern then you will not be able to build an MVC application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 16, 2019 1:29 PM
  • User711641945 posted

    Hi bootzilla,

    According to your request, do you want to get the data of user but only map two properties of Username and Enabled?

    There are two ways to get the ViewModel:

    1.Use foreach loop :

    • UserViewModel.cs :
     public class UserViewModel
        {
            public string Username { get; set; }
            public bool Enabled { get; set; }
        }
    • Action :
    public IActionResult Index()
            {
                var user = _userService.GetUsers();
                var viewModel = new List<UserViewModel>();
                foreach (var users in user)
                {
                    viewModel.Add(new UserViewModel { Username = users.Username,Enabled=users.Enabled });
                };
                return View(viewModel);
            }

    2.Use AutoMapper :

    public IActionResult Index()
            {
                var user = _userService.GetUsers();
                var viewModel = _mapper.Map<List<UserViewModel>>(user);
                return View(viewModel);
            }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2019 6:56 AM