2012-05-12 56 views
0

路由值在Post控制器,URL是這樣的:獲取AJAX動作+ Asp.net MVC 3

 http://127.0.0.1/post/5006/some-text-for-seo-friendly 
    {contoller}/{id}/{seo} 

    public ViewResult Index(){ 
    ..... 
    } 

我在索引視圖中使用Ajax.BeginForm並將其映射到相同的控制器AddComment動作。

@using (Ajax.BeginForm("AddComment", "Post", new AjaxOptions() 
        { 
         HttpMethod = "GET", 
         InsertionMode = InsertionMode.InsertAfter, 
         UpdateTargetId = "comment-container" 
      })) 
      { 
       <textarea cols="2" rows="2" name="comment" id="comment"></textarea> 
       <input type="submit" value="Add Comment" /> 
      } 

和控制器

public PartialViewResult AddComment(string comment){ 
       // how can I get 5006 {id} here 
    } 

我的問題是如何能得到{id} [5006]AddComment行動。

注意:困難的方法是使用Request.UrlReferrer並拆分'/'並選擇表單數組。

回答

1

您需要的id提供使用this overloadBeginForm方法,這需要routeValues參數:

@using (Ajax.BeginForm("AddComment", "Post", 
    new { id = 5006 }, 
    new AjaxOptions 
    { 
    ... 

那麼你就應該能夠採取id作爲動作方法的參數:

public PartialViewResult AddComment(int id, string comment) 
{ 
    ... 

MVC將使用填充的id值調用AddComment

+0

據我所知,「Ajax Action」的參數被映射爲「Ajax.BeginForm」中使用的html標籤的名稱。你的意思是我應該在Ajax Form中使用一些隱藏字段並將值設置爲'5002'? – Mironline

+0

尼古拉斯巴特勒,請你再次檢查問題。 – Mironline

+0

@Mironline我已更新我的答案 –