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 8060759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:05:50+00:00 2026-06-05T10:05:50+00:00

Im trying to populate a viewmodel based on the value from the querystring. Heres

  • 0

Im trying to populate a viewmodel based on the value from the querystring. Heres my controller:

public ViewResult Index(int? ShiftStatusID)
{
    //Get logged in User
    User user = _accountService.GetUser(_formsAuthService.GetLoggedInUserID());

    if (ShiftStatusID == null) // Get all shifts
    {
        ViewModelShiftList viewModel = new ViewModelShiftList
        {
            Shifts = _shiftService.GetShifts(user.organisationID.Value).ToList()
        };
    }
    else // Get shifts by status id
    {
        ViewModelShiftList viewModel = new ViewModelShiftList
        {
            Shifts = _shiftService.GetShiftsByStatus(user.organisationID.Value, ShiftStatusID).ToList()  
        };
    }

    return View(viewModel);
}

So it wont let me return the viewmodel to the view, saying “viewmodel does not exist in the current context”. It wont let me declare the view model outside the if statement. How should this be done?

  • 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-05T10:05:51+00:00Added an answer on June 5, 2026 at 10:05 am

    Try to move out if statement in your viewresult.
    Here is a code example which should be working fine:

    public ViewResult Index(int? ShiftStatusID)
    {
           //Get logged in User
            User user = _accountService.GetUser(_formsAuthService.GetLoggedInUserID());
            var viewModel = new ViewModelShiftList();
    
            if (ShiftStatusID.HasValue)// Get shifts by status id
            {
                viewModel.Shifts = _shiftService.GetShifts(user.organisationID.Value).ToList();
            }
            else // Get all shifts
            {
                viewModel.Shifts = _shiftService.GetShiftsByStatus(user.organisationID.Value, ShiftStatusID).ToList();
            }
    
        return View(viewModel);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to populate a JSF Datatable with data that I am retrieving from
I am trying to populate a collection with data from a JSON file. I'm
I am trying to populate a sql variable with a value. It works when
I am trying to secure a controller action to prevent a user from accessing
iam trying to populate the HyperlinkButton.NavigateUri Property with a string from the properties... But
I'm trying to populate a listview from another class, but I'm geting this error:
I am trying to populate the dropdown value with the selected value. I referred
I'm trying to get a DataGridComboBoxColumn working with my ViewModel. Everything appears to work
I'm trying to pass my View an instance of the following ViewModel: public class
I am trying to populate a string with a double value using a sprintf

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.