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 为什么我的MVC 3表单没有发布到正确的控制器操作?_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 为什么我的MVC 3表单没有发布到正确的控制器操作?

Asp.net mvc 3 为什么我的MVC 3表单没有发布到正确的控制器操作?,asp.net-mvc-3,Asp.net Mvc 3,我有一个视图,通过Index()操作可以很好地渲染。 基本上,我从数据库读取数据并加载到我的模型中 当用户填写或更改值并点击提交时,我希望表单被发布到同一控制器上的不同操作 但是表单被提交到默认的index()操作 最终目标是让用户保持在同一页面上,但显示提交的结果 这是我的密码: 区域注册以注册路线: context.MapRoute( "Test", "app/test/{Id}", new {

我有一个视图,通过Index()操作可以很好地渲染。 基本上,我从数据库读取数据并加载到我的模型中

当用户填写或更改值并点击提交时,我希望表单被发布到同一控制器上的不同操作

但是表单被提交到默认的index()操作

最终目标是让用户保持在同一页面上,但显示提交的结果

这是我的密码:

区域注册以注册路线:

context.MapRoute(
                "Test",
                "app/test/{Id}",
                new { controller = "Test", action = "Index" }
            );
控制器:

[HttpGet]
public ActionResult Index(string rk, string g){
  TestModel model = new TestModel();
  //Now read from DB & populate the model
  return View(model);  
}
这是我希望提交以发布的操作:

[HttpPost]
public ActionResult Update(TestModel model)
{
    model = new TestModel();
    model.Status = "Form Posted!";

    return View("Index", model);
}
以下是我的看法:

@using (Html.BeginForm("Update", "Test", FormMethod.Post, new { id="frmUpdate"}))
{
   //My form fields get populated by model
   <button type="submit" id="btnUpdate" onclick="return ValidateAndSubmit(this.id, 'result_label');">Update</button>
}
我做错了什么

编辑:
我无法执行$.ajax()post,因为表单上有一个recaptcha。

您的路由不允许操作参数,因此发送到该控制器的任何内容都将路由到您提供的默认操作,即
索引。请尝试以下操作:

context.MapRoute(
    "Test",
    "app/test/{action}",
    new { controller = "Test", action = "Index" }
);

这将
/app/test
路由到
索引
操作,并
/app/test/update
路由到
更新
操作。

您的路由不允许操作参数,因此发送到该控制器的任何内容都将路由到您提供的默认操作,即
索引
。请尝试以下操作:

context.MapRoute(
    "Test",
    "app/test/{action}",
    new { controller = "Test", action = "Index" }
);

这将把
/app/test
路由到
索引
操作,把
/app/test/update
路由到
更新
操作。

可以是这样的两个路由来解决404问题:

        context.MapRoute(
            "Test1",
            "app/test/{Id}",
            new { controller = "Test", action = "Index" }
        );

        context.MapRoute(
            "Test2",
            "app/test/{action}",
            new { controller = "Test", action = "Update" }
        );

可能有两条这样的路线来解决404问题:

        context.MapRoute(
            "Test1",
            "app/test/{Id}",
            new { controller = "Test", action = "Index" }
        );

        context.MapRoute(
            "Test2",
            "app/test/{action}",
            new { controller = "Test", action = "Update" }
        );

现在我无法访问Index()操作。所以页面加载现在失败了。我需要两条路线吗?一个用于索引,一个用于更新()?我得到这个错误:Description:HTTP404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下URL并确保其拼写正确。否,上面的路由代码所说的是“如果请求URL中的
/app/test
之后有任何内容,请使用相应的名称调用
测试
控制器中的操作方法;如果没有,则转到默认值,
索引
”。您请求尝试访问索引视图的实际完整url是什么?现在我无法访问index()操作。所以页面加载现在失败了。我需要两条路线吗?一个用于索引,一个用于更新()?我得到这个错误:Description:HTTP404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下URL并确保其拼写正确。否,上面的路由代码所说的是“如果请求URL中的
/app/test
之后有任何内容,请使用相应的名称调用
测试
控制器中的操作方法;如果没有,则转到默认值,
索引
”。您请求尝试访问索引视图的实际完整url是什么?