2012-12-09 67 views
0

我应该如何做到这一点而不使用RouteData.Values [“id”];使用ActionLink发送数据GET电话

我使用这个行动呼吁从视图

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

这是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); 
    } 

是有这样做的更好的办法?

回答

2

好了,这样做的通常的方法是:

public ActionResult Create(string id) 
{ 
    int intID; 

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

    //... 
} 
+0

谢谢你,接受你的awnser – Darkmage

+1

你是欢迎。您可以根据需要添加任意数量的参数。借助ASP.NET MVC,您还有另一个优势。网址末尾的任何值都会传递给id参数,例如*“/ Post/Create/23”*。祝你今天愉快。 –

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

不要忘了,你可以写HTML太:<a href="/Post/Create">Post</a>

相关问题