balanv
balanv

Reputation: 10898

How to pass and receive data in url using ASP.NET MVC 3 Razor

I am new to ASP.NET MVC 3, i am trying to pass and retrieve data from controller

like if the below is the url

href="http://www.example.com/mycontroller/myaction/22"

how do i receive value 22 in a controller and pass it on to view?

Upvotes: 2

Views: 2612

Answers (2)

Roy Dictus
Roy Dictus

Reputation: 33139

Read a good book or a series of introductory articles and blog posts on ASP.NET MVC 3 first. You'll be glad you did. MVC 3 is not easy to learn if you're from an ASP.NET background.

Upvotes: 0

Ben Foster
Ben Foster

Reputation: 34800

You could do with learning how routing works in ASP.NET MVC. There are some great tutorials here.

But, based on your example, using the Default route that is added as part of the ASP.NET MVC project template, if you created the following action method on a controller named MyController you could receive your parameter:

public ActionResult MyAction(int id){
    // do something with id
    ViewBag.SomeId = id; // can be accessed by calling ViewBag.SomeId on your view
    return View(id); // can be accessed via the Model property of your view
}

Before you dive into solving problems with ASP.NET MVC it's crucial to learn the fundamentals. If you're coming from ASP.NET web forms then it's quite a learning curve.

Upvotes: 6

Related Questions