简体   繁体   English

ASP MVC和更改发布表单的URL

[英]ASP MVC and changing url of posted form

I have an MVC 4 app and I am using a RESTful methodology for my URLs. 我有一个MVC 4应用程序,并且正在为我的URL使用RESTful方法。 I have the following routes registered in my app (along with others that are not relevant to my question: 我在我的应用中注册了以下路线(以及与我的问题无关的其他路线:

    //EDIT
    routes.MapRoute(alias + "_EDIT", alias + "/{id}/edit",
                    new { controller = controllerName, action = "edit" },
                    new { httpMethod = new RestfulHttpMethodConstraint(HttpVerbs.Get) });

    //PUT (update)
    routes.MapRoute(alias + "_PUT", alias + "/{id}",
                    new { controller = controllerName, action = "update" },
                    new { httpMethod = new RestfulHttpMethodConstraint(HttpVerbs.Put) });

I have the following methos in my controller mapping to these routes: 我的控制器中有以下方法映射到这些路由:

public override ActionResult Edit(int id)
{...}

public override ActionResult Update(RequestEditViewModel userModel)
{
   if (!ModelState.IsValid)
   {
    //do some stuff to ensure lookups are populated
    ...
        return View("Edit", userModel);
   }
}

In my app when I perform a request to edit a request my URL looks like: 在我的应用中,当我执行请求以编辑请求时,我的URL如下所示:

http://server/request/1/edit

it correctly calls the Edit method on my controller. 它会正确调用控制器上的Edit方法。

My Edit.cshtml uses the followng to ensure the Update method is called on PUT: 我的Edit.cshtml使用以下代码来确保在PUT上调用Update方法:

@using (Html.BeginForm("Update", "Request"))
{
    @Html.HttpMethodOverride(HttpVerbs.Put);
    ...
}

My form is generated as follows: 我的表格生成如下:

<form action="/requests/71" method="post" autocomplete="off" novalidate="novalidate">
<input name="X-HTTP-Method-Override" type="hidden" value="PUT"/>
...
</form>

When I click the submit button it correctly calls my Update method. 当我单击提交按钮时,它会正确调用我的Update方法。

OK...Now for the issue. 好的...现在解决这个问题。 If my model is NOT valid I want to return back the Edit model. 如果我的模型无效,我想返回编辑模型。 As you can see in the above code but, the URL is the one called from the submit button: 正如您在上面的代码中看到的那样,URL是从“提交”按钮调用的URL:

http://server/request/1

not

http://server/requests/1/edit

I have tried an reviewed two other options but both of these redirect the request back through the Edit method again which adds additional overhead and also puts all the model values in the querystring which I do NOT want: 我尝试了另外两个选项,但是这两个选项都再次通过Edit方法将请求重定向回去,这增加了额外的开销,并且还将所有模型值放入我不想要的querystring中:

return RedirectToAction("Edit", userModel);
return RedirectToRoute("requests_Edit", userModel);

So, is there a way to just return the View as I have in my code but, ensure the URL changes back and include the "/edit"? 因此, 是否有一种方法可以像我在代码中那样仅返回视图,但是要确保URL变回来并包含“ / edit”?

The only alternative I have come up with is to perform an AJAX call and put the update that way the URL never changes, but I was trying to avoid that for this form. 我想出的唯一选择是执行AJAX调用,并以URL永不更改的方式进行更新,但是我试图避免这种形式的更新。

Conceptually, you want to be doing something like a Server.Transfer (that is, making on URL appear to be another.) This discussion may be of use to you: 从概念上讲,您想要做类似Server.Transfer的操作(也就是说,使URL看起来像是另一种操作。)此讨论可能对您有用:

How to simulate Server.Transfer in ASP.NET MVC? 如何在ASP.NET MVC中模拟Server.Transfer?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM