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

  • Home
  • SEARCH
  • 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 8434215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:35:47+00:00 2026-06-10T06:35:47+00:00

The following action does not require a querystring, but it does require the Id

  • 0

The following action does not require a querystring, but it does require the Id to be passed in the URL.

    public ViewResult Details(int id)
    {
        Domain domain = db.Domains.Find(id);
        return View(domain);
    }

How do I change this so the name can be passed in the URL instead of the Id?

When I change that to the following, it produces an error “Sequence contains no elements” regardless of how I attempt to execute it.

    public ViewResult Details(String name)
    {
        Domain domain = db.Domains.Where(d => d.Name == name).First();
        return View(domain);
    }

Any help is greatly appreciated.

  • 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-10T06:35:48+00:00Added an answer on June 10, 2026 at 6:35 am

    “Sequence contains no elements” error is because your LINQ query is not returning any results for this where clause but you are trying to Apply the First() function on the result set (which is not available for your where condition in this case).

    Use the First() function when you are sure that there is Atleast one element available in the resultset you are applying this function on. If there are no elements as the result of your LINQ expression, Applying First will throw the above error.

    For the URL to have the name parameter instead of the integer id, just change the parameter type to string. Keep the variable name as id itself. because that is what the MVC Routing will use to help you write those pretty URLS.

    So now your URL can be like

    ../SomeController/Details/someName
    

    Where someName is going to be the parameter value of your Details action method.

    I would use the FirstOrDefault and do a checking to see whether an element is available for the LINQ expression.FirstOrDefault will returns the first element of a sequence, or a default value if the sequence contains no elements.

    public ViewResult Details(String id)
    {
        Domain domain = db.Domains.Where(d => d.Name == id).FirstOrDefault();
        if(domain!=null)
           return View(domain);
        else
           return View("NotFound")  //Lets return the Not found View 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm exploring alfresco action tag. Following action tag does not specify permission <action id=document-copy-to
I have the following action in my ASP.NET Web API: public IEnumerable<Car> carssOfUser(int id,
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
What does the path in an action of the following action-mapping mean .Does it
What actions does the IE take when we right click the following URL and
I've got the following action on an ApiController: public string Something() { return value;
I have the following action methods: public ActionResult ProfileSettings() { Context con = new
I have a controller with the following action code: public JsonResult CheckPasswordStrength(string password, string
In my controller i have an action which does not have a corresponding view.
I want to update Partial View via ajax, but it does not work. Look

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.