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 )
{
...