0

I was looking for a way to load different data from the database based on an ID or name in the URL.

For example:

This is the link to the view:

http://localhost:50830/CreateSale/Order

Now I want something like this:

http://localhost:50830/CreateSale/Order/5
OR
http://localhost:50830/CreateSale/Order/Cookies

And then based on the ID (5) or the name (Cookies), the controller CreateSale will load the data that is linked to the ID/name in the Order view. This data will be different for every ID/name.

EDIT

So basically what I want is a way to create a URL like the one I showed. So when I type the link into the browser, depending on the the last part (5 or Cookie), it should show the desired data on the page.

For example:

If the link is:

http://localhost:50830/CreateSale/Order/Cookies

Then I want to see data about ordering cookies on the page.

When the link is:

http://localhost:50830/CreateSale/Order/Toys

Then I want to see data about ordering toys on the page.

Is there any way to do this in asp.net mvc4?

Thanks!

1 Answer 1

1

This should work out of the box with the default routing. On the controller method read the Id and pass it to your method that reads from the database, then pass the results back to the view.

public ActionResult Order(string id)
{
    var data = DbLoader.Execute(id);
    return View(data);
}

On your view, read the data you passed to it. e,g in Index.cshtml.

@foreach (var dataItem in Model)
{
    //Display something
}

This assumes you do not have a custom route for "CreateSale/Orders" and is using the default route of :

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

If you do have a custom route for "/CreateSale/Order" ensure the url ends with "/{id}" like the default route.

EDIT

To generate urls use the Url.Action method either in your view or controller. E.g

@Url.Action("Order", "CreateSale", new RouteValueDictionary { { "id","5" } }, Request.Url.Scheme, Request.Url.Host) //generates http://localhost:50830/CreateSale/Order/5

@Url.Action("Order", "CreateSale", new RouteValueDictionary { { "id","Cookies" } }, Request.Url.Scheme, Request.Url.Host) //generates http://localhost:50830/CreateSale/Order/Cookies

@Url.Action("Order", "CreateSale", new RouteValueDictionary { { "id","Toys" } }, Request.Url.Scheme, Request.Url.Host) //generates http://localhost/CreateSale/Order/Toys
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, but is there a way to create a URL like the one I showed in the post. So when I type the link into the browser, depending on the the last part (5 or Cookie), it should who the desired data.
Are you trying to create links or are you trying to display data based on the url provided to the browser?
I'm trying to display data based on the url, but I'm also looking for way to create such links, a way to put an ID or name in the link. I edited the link, i hope is it's a bit more clear.
If you're trying to generate links in the view or controller use the Url.RouteUrl or Url.Action helper. I've edited my answer with code to demonstrate this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.