Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在ajax操作中获取路由值+;Asp.NETMVC3_Asp.net Mvc 3_C# 4.0_Routing - Fatal编程技术网

Asp.net mvc 3 在ajax操作中获取路由值+;Asp.NETMVC3

Asp.net mvc 3 在ajax操作中获取路由值+;Asp.NETMVC3,asp.net-mvc-3,c#-4.0,routing,Asp.net Mvc 3,C# 4.0,Routing,在PostController中,URL如下所示: http://127.0.0.1/post/5006/some-text-for-seo-friendly {contoller}/{id}/{seo} public ViewResult Index(){ ..... } 我在索引视图中使用了Ajax.BeginForm,并将其映射到同一控制器中的AddComment操作 @using (Ajax.BeginForm("AddComme

Post
Controller中,URL如下所示:

     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" />
            }
我的问题是如何在
AddComment
操作中获得
{id}[5006]


注意:困难的方法是使用
Request.urlreferer
并按
'/'
分割,然后选择表单数组。

您需要将
id
提供给
BeginForm
方法,该方法使用
routeValues
参数:

@using ( Ajax.BeginForm( "AddComment", "Post",
  new { id = 5006 },
  new AjaxOptions
  {
    ...
然后,您应该能够将id作为操作方法的参数:

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

MVC将使用填充的id值调用
AddComment

据我所知,
Ajax操作
的参数映射为
Ajax.BeginForm
中使用的html标记名称。你的意思是我应该在Ajax表单中使用一些隐藏字段并将值设置为
5002
?Nicholas Butler,请你再次检查这个问题。
public PartialViewResult AddComment( int id, string comment )
{
  ...