Search code examples
asp.nethtml.actionlink

Sending data with ActionLink GET call


How should i do this without using the RouteData.Values["id"];

I am using this action call from the view

@Html.ActionLink("Post","Create","Post", new {id=item.Id }, null)

And this is the Action

    // GET: /Post/Create
    public ActionResult Create()
    {
        var x = (string)RouteData.Values["id"];
        var model = new Post() { DateCreated = DateTime.Now, BlogID = int.Parse(x)};

        return View(model);
    } 

is there a better way of doing this?


Solution

  • Well, the usual way of doing this is:

    public ActionResult Create(string id)
    {
        int intID;
    
        if (int.TryParse(id, out intID)) {
            //...   
        }
    
        //...
    }