Question

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?

Était-ce utile?

La solution

Well, the usual way of doing this is:

public ActionResult Create(string id)
{
    int intID;

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

    //...
}

Autres conseils

@Html.ActionLink("Post","Create","Post")

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top