Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:56:25+00:00 2026-06-12T17:56:25+00:00

I have a view model that contains user details and also extended user details

  • 0

I have a view model that contains user details and also extended user details from two different models called User and UserDetails.

In my UsersController I have the following for my Details method –

public ViewResult Details(RegisterViewModel viewModel)
        {

            User currentuser = context.Users
                .Include("UserDetails")
                .Where(i => i.UserName == viewModel.UserName)
                .First();

            currentuser.UserDetails = new UserDetails();

            return View(currentuser);   
        }

In my Details view I start with –

@model TRS.ViewModels.RegisterViewModel

and then try to list the detials from the viewmodel e.g.

<tr>
    <td>User Name</td>
    <td>@Model.UserName</td>

</tr>

but when I go to the details page for a user I get this error –

Server Error in '/' Application.

Sequence contains no elements

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Sequence contains no elements

Source Error: 


Line 37:         {
Line 38:             
Line 39:             User currentuser = context.Users
Line 40:                 .Include("UserDetails")
Line 41:                 .Where(i => i.UserName == viewModel.UserName)

I’m obviously doing something wrong but haven’t been able to find what. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:56:26+00:00Added an answer on June 12, 2026 at 5:56 pm

    There are many things you are doing wrong. The first one is with your LINQ query in which you are searching for a record in your database whose UserName matches viewModel.UserName. But if this query doesn’t return any results when you attempt to call the .First() method at the end you will get an exception. So you could test whether the query returned results:

    User currentuser = context.Users
        .Include("UserDetails")
        .Where(i => i.UserName == viewModel.UserName)
        .FirstOrDefault();
    if (currentuser == null)
    {
        // no records were found in the database that match this query
        return HttpNotFound();
    }
    

    Another thing that is wrong with your code is that your view is strongly typed to TRS.ViewModels.RegisterViewModel but your Details controller action is passing a User model to this view which cannot work. You need to pass a view model to it.

    Also it is not very clear how is this Details action invoked and what’s the value of the view model passed as argument. Are you sure that you are passing any values to the request? Otherwise all the properties will be empty which might explain by the way why your LINQ query doesn’t find any records.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model that contains a Product class type and an IEnumerable<
I have a View that contains a ParentModel, which contains 2 Models. I only
I have a User model that contains information such as username, email, hometown, etc.
I have a view model that looks like this public class ViewModelRound2 { public
I have a view model that is displayed in a view that uses uploadify.
I have two forms: one that takes user information (name, birthdate, etc): formA. And
I currently have a view that contains a ListBox bound to a a collection
Lets say I have some view models set up as follows: public class User
My scenario is this: I have two listbox's, one that contains all my database
I have a view with a grid that contains items added to a workstation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.