0

RouteData.Values["id"]; を使用せずにこれを行うにはどうすればよいですか?

ビューからこのアクション コールを使用しています

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

そしてこれがアクション

    // 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);
    } 

これを行うより良い方法はありますか?

4

2 に答える 2

2

さて、これを行う通常の方法は次のとおりです。

public ActionResult Create(string id)
{
    int intID;

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

    //...
}
于 2012-12-09T21:14:54.623 に答える
1
@Html.ActionLink("Post","Create","Post")

忘れないでください。いつでも HTML も記述できます。<a href="/Post/Create">Post</a>

于 2012-12-09T21:22:53.393 に答える