How to access a specific column in a database using multiple view model

Viewed 53

I want to access multiple models from a database on a single view. I created a view model class, composed of an IEnumerable of both of my models that represents two tables in my database. My problem comes into play when it says

CS106 IEnumerable' does not contain a definition for 'ProfilePicture' and no accessible extension method 'ProfilePicture' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)

The way I'm calling it is by typing @Model.ApplicationUserVM.ProfilePicture. If that doesn't work, how else am I supposed to retrieve the information?

View:

@using Website_friend_feature.Models;
@model ProfilePictureViewModel
@{
            Layout = null;


}


<!DOCTYPE html>
<html>
<head>



.....  




  <!-- The Grid -->
  <div class="w3-row">
    <!-- Left Column -->
    <div class="w3-col m3">
      <!-- Profile -->
      <div class="w3-card w3-round w3-white">
        <div class="w3-container">
         <p class="w3-center"><img src=@Model.ApplicationUserVM.ProfilePicture class="w3-circle" style="height:106px;width:106px" alt="Avatar"></p>
         <h4 class="w3-center" style="margin-bottom: 0px;">@Model.ApplicationUserVM.FirstName @Model.ApplicationUserVM.LastName</h4>
         <h2 style="color: lightgray; text-align: center; font-size: 9px; margin-top: 0px; "> @@@Model.ApplicationUserVM.UserName</h2>
         

Models:

ApplicationUser:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;

namespace Website_friend_feature.Areas.Identity.Data
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
       [PersonalData]
       [Column(TypeName = "nvarchar(100)")]
        public string FirstName { get; set; }

        [PersonalData]
        [Column(TypeName = "nvarchar(100)")]
        public string LastName { get; set; }

        [PersonalData]
        [Column(TypeName = "nvarchar(1000)")]
        public string ProfilePicture { get; set; }

    }
}

ProfilePicture:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace Website_friend_feature.Models
{
    public class ProfilePicture
    {
        [ForeignKey("Id")]
        public string Id { get; set; }

        [NotMapped]
        [DisplayName("Upload Profile Picture")]
        public IFormFile ImageFile { get; set; }

        public string Picture { get; set; }
    }
}

ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Website_friend_feature.Areas.Identity.Data;

namespace Website_friend_feature.Models
{
    public class ProfilePictureViewModel
    {
        public IEnumerable<ProfilePicture> ProfilePictureVM{ get; set; }
        public IEnumerable<ApplicationUser> ApplicationUserVM { get; set; }
    }
}

controller:

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Website_friend_feature.Areas.Identity.Data;
using Website_friend_feature.Models;

namespace Website_friend_feature.Controllers
{
    public class UserController : Controller
    {

        
        private readonly UserManager<ApplicationUser> _userManager;
       

        public UserController (UserManager<ApplicationUser> userManager)
        {
            
            _userManager = userManager;

            
        }
        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Profile(String id) 
        
        {
            

            
            if (id == null)
            {
                return BadRequest();
            }
            var user = await _userManager.FindByNameAsync(id);
            if (user is null)
            {
                return NotFound();
            }

            return View(user);
        }
        

    }
}
1 Answers

The message is clear. You can't directly access the ProfilePicture from an IEnumerable<ApplicationUserVM>.

Instead, you need to get a specific T item from IEnumerable<T> in order to access the properties from T.

Either one of these approaches:

With indexer:

@Model.ApplicationUserVM[0].ProfilePicture

Note: Make sure that the indexer is valid for the IEnumerable, array/list, otherwise, you would get IndexOutOfRangeException.


With System.Linq

@Model.ApplicationUserVM.Single().ProfilePicture

Note: Using .Single() would throw exception if IEnumerable contains more than one element.

@Model.ApplicationUserVM.First().ProfilePicture

With loop:

@foreach (var user in Model.ApplicationUserVM)
{
    <div>@user.ProfilePicture</div>
}

Same goes for the rest that you are trying to access the property from T item.

Related