0

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?

Darkmage
  • 1,583
  • 9
  • 24
  • 42

2 Answers2

2

Well, the usual way of doing this is:

public ActionResult Create(string id)
{
    int intID;

    if (int.TryParse(id, out intID)) {
        //...   
    }

    //...
}
Nightly Coder
  • 121
  • 1
  • 6
  • 1
    You are welcome. You can add as many parameters as you wish. With ASP.NET MVC you have another advantage. Any value at the end of an url is passed to the id parameter, for instance *"/Post/Create/23"*. Have a nice day. – Nightly Coder Dec 09 '12 at 21:28
1
@Html.ActionLink("Post","Create","Post")

don't forget, you can always write HTML too: <a href="/Post/Create">Post</a>

viperguynaz
  • 12,044
  • 4
  • 30
  • 41