سؤال

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?

هل كانت مفيدة؟

المحلول

Well, the usual way of doing this is:

public ActionResult Create(string id)
{
    int intID;

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

    //...
}

نصائح أخرى

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top