Lith
Lith

Reputation: 1325

DataTextField combination from 2 columns

I have a simple model class User (Data annotations removed for simplicity):

public int Id { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public string PhoneNumber { get; set; }
public int Nr { get; set; }

And model Registration (again, data annotations removed for simplicity):

public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public DateTime BookTaken { get; set; }
public DateTime BookReturned { get; set; }

The Registration model uses UserId, therefor i created a select inside my form in cshtml file:

<select asp-for="@Model.registration.UserId" asp-items="@Model.UsersOptions"></select>

The @Model comes from my ViewModel:

public IEnumerable<Registration> registrations { get; set; }
public Registration registration { get; set; }
public IEnumerable<User> users { get; set; }
public SelectList UsersOptions { get; set; }

And the viewModel is being passed from the Controllers Method:

    public ViewResult Index(int? Id)
    {
        RegistrationsIndexViewModel viewModel = new RegistrationsIndexViewModel()
        {
            registrations = _registrationRepository.GetAllRegistrations(),
            registration = _registrationRepository.GetRegistration(Id ?? 0),
            users = _userRepository.GetAllUsers(),
            UsersOptions = new SelectList(_userRepository.GetAllUsers(), "Id", "Name"),
        };
        return View(viewModel);
    }

ALL of this (including the repositories that the code i did not show, since its not relevant to the question) works fine. BUT UsersOptions = new SelectList(_userRepository.GetAllUsers(), "Id", "Name"), Makes the Html select list that only contains Name values. I want it to contain Name AND Lastname values from my User model.

If i use new SelectList(_userRepository.GetAllUsers(), "Id", "Name") OR new SelectList(_userRepository.GetAllUsers(), "Id", "Lastname") - works fine. But when i try to combine both variables together new SelectList(_userRepository.GetAllUsers(), "Id", "Name" + "Lastname") - I get an error NullReferenceException: Object reference not set to an instance of an object.

I DO understand why. Its because it tried to find a column "NameLastname" instead of "Name" and "Lastname" separate. But HOW do i make it so in html my select list would contain both variable values (name and lastname) instead of just one of them.

is there away without using foreach loop inside cshtml?

Upvotes: 0

Views: 120

Answers (1)

TanvirArjel
TanvirArjel

Reputation: 32099

I want it to contain Name AND Lastname values from my User model.

You can do as follows:

public ViewResult Index(int? Id)
{

    var users = _userRepository.GetAllUsers();

    var usersForSelectList = users.Select(u => new 
    {
       Id = u.Id,
       DisplayName = u.Name + " " + u.LastName
    }).ToList();

    RegistrationsIndexViewModel viewModel = new RegistrationsIndexViewModel()
    {
        registrations = _registrationRepository.GetAllRegistrations(),
        registration = _registrationRepository.GetRegistration(Id ?? 0),

        UsersOptions = new SelectList(usersForSelectList , "Id", "DisplayName"),
    };

    return View(viewModel);
}

Upvotes: 1

Related Questions