【发布时间】:2018-03-13 15:25:59
【问题描述】:
我有以下代码:
GET方法:
public async Task<ActionResult> EditUser(string username)
{
// something here
}
POST方法:
[HttpPost]
public async Task<ActionResult> EditUser(UserEditViewModel model)
{
if (ModelState.IsValid)
{
// all right, save and redirect to user list
return RedirectToAction("UserList");
}
// something wrong with model, return to form
return View(model);
}
它工作正常,但在浏览器的 URL 中获取参数username=bla-bla-bla 丢失了。因此,用户无法复制该链接以再次打开此页面。是否可以恢复 URL 参数?如果我确实重定向,那么我会因错误而丢失模型......
【问题讨论】:
-
不完全清楚什么不起作用。你是说如果你输入“localhost:1234/User/EditUser?username=john”,“john”就不会进入 Get 操作?
-
我的意思是如果用户点击“刷新”然后这个页面将不会打开。或者他无法将其添加到收藏夹,复制并粘贴到其他标签,发送给朋友......
-
所以
localhost:1234/User/EditUser?username=john会加载地址栏中没有?username=john的页面? -
如果你的意思是你的方法的参数消失了,只需将它添加到你的帖子中。
public async Task<ActionResult> EditUser(UserEditViewModel model, string username)- 但是,请注意,这可能会使某人轻松更改用户名。如果您的意思是当您重定向到 UserList 时会丢失参数,那么您需要将这些参数添加到重定向的路由参数中。另外,请确保您的 FORM 方法 url 包含您的参数。
标签: c# asp.net-mvc asp.net-mvc-5 viewmodel asp.net-mvc-5.2