Mr.Smithyyy
Mr.Smithyyy

Reputation: 1329

MVC5 Retrieving Data from Database

I have created a controller with basic CRUD operations to handle my database and I wanted to add in a custom search function to search by name.

I tried to base my code off this because it's essentially the same and this one works fine.

// GET: Users/Details/5
public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    User user = db.Users.Find(id);
    if (user == null)
    {
        return HttpNotFound();
    }
    return View(user);
}

I'm trying to get a result to show up if I add a name to the url instead of the id.

// GET: Users/UserSearch/Smith
public ActionResult UserSearch(string name)
{
    if (name == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    User user = db.Users.Find(name);
    if (user == null)
    {
        return HttpNotFound();
    }
    return View(user);
}

When I try to use it I get:

Error Image

Edit I have also tried this but to no avail:

public ActionResult UserSearch(string name)
{
    var user = from u in db.Users
               where u.UserName == name
               select u;

    return View(user);
}

And the error this throws:

enter image description here

Upvotes: 0

Views: 807

Answers (2)

Win
Win

Reputation: 62260

In order for UserSearch action method to work, you need to configure a route. For example,

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        /* You need this */
        routes.MapRoute(
            name: "Name",
            url: "{controller}/{action}/{name}",
            defaults: new { controller = "Home", action = "Index", 
                id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = 
                UrlParameter.Optional }
        );
    }
}

Another Error - View is not found

The view 'UserSearch' is not found means you do not have UserSearch view. You need to create one.

Upvotes: 1

dakre18
dakre18

Reputation: 191

As a different way to try to create it, have you tried creating the model for it, then create a controller using that model? You don't have to, but i know creating a model with the properties you want stored in the database is one way to have it do the work for you.

Just a thought, but the other problem is that does not tie into the user authentication (if that is what you are aiming for).

I would suggest creating a second instance of MVC to test out the model creation. Just put properties in like public string name { get;set; } and anything else you want it to keep track of.

Edit: I think it would have been better to include the other steps. I don't remember them as clearly from this point, but once you have a model created with the properties you want, you create a controller based off of that model. This will create the controller and view that it needs to run.

Upvotes: 0

Related Questions