简体   繁体   中英

Access data from Asp.Net MVC URL

So if I have a URL such as mysite.com/user/1234/show which object can I use in the view that is displayed to get at the 1234 int in the URL above? For instance I want to have an action link that uses the user id again for the next controller action call such as mysite.com/user/1234/edit.

Thanks

You shouldn't have to go directly to the URL for the data. Instead, add a parameter to your route and action. Your route could look something like "{controller}/{id}/{action}" and your action something like:

    ViewResult Show(int userId){
      var viewData = new MyViewData(){
        UserID = userId
      };

      return View(viewData);
    }

Then, in your view, use Model.UserID .

EDIT : This is initially more work than just doing a (dare I write it?)

Int32.Parse(Request["userId])

in your view. However, the approach I have shown above is the preferred approach, since you will be leveraging the power of MVC's routing and binding capabilities. If you want to change parameter name someday, you just need to fix your route and the action's parameter name, as opposed to having to sort through all of your application searching for places where you pull values directly from the Request collection.

It depends upon your your routing scheme. have look at these tutorial

Creating Custom Routes

mysite.com/user/show/1234

                routes.MapRoute(
                "User",                                        // Route name
                "user/show/{ID}",                              // URL with parameters
                new { controller = "User", action = "show" }   // Parameter defaults
            );

above url wiil hopefully call, 'show' Action for 'user' controller and pass the ID, 1234

I believe you can get at it via the RouteData property of the request context. But a much easier way would be to just add a field for it to your model and have the controller set it there.

Change the default route in your global.asax.cs to -

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{id}/{action}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

Create a UserController with a Show method like this -

public class UserController : Controller
{
    public ActionResult Show(int id)
    {
        var model = new UserViewModel {Id = id};
        // Retrieve user from data layer and update model with other user details here
        return View(model);
    }

    public ActionResult Edit(int id)
    {
        // Deal with edit action in here
    }
}

public class UserViewModel
{
    public int Id { get; set; }
}

In your aspx view, make sure that you page inherits from ViewPage<UserViewModel> by declaring in the page directive of your aspx view -

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<UserViewModel>" %>

Then you can create an edit link in your page like this -

<%=Html.ActionLink("Edit User", "Edit", new { id = Model.Id }) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM