在RedirectToAction控制器内调用时,它会使用HTTP GET自动重定向.如何明确告诉它使用HTTP POST?
我有一个接受GET和POST请求的动作,我希望能够RedirectToAction使用POST并发送一些值.
像这样:
this.RedirectToAction(
"actionname",
new RouteValueDictionary(new { someValue = 2, anotherValue = "text" })
);
Run Code Online (Sandbox Code Playgroud)
我希望使用HTTP POST而不是GET发送someValue和anotherValue值.有谁知道如何做到这一点?
我有一个控制器,可以处理三个特定于我的问题的操作.
第一个是编辑操作,它返回一个HTML表单的视图,用户可以编辑给定项目的属性.
第二个是更新操作,它接受从浏览器返回的帖子并更新数据库.更新成功后,我们会重定向到操作.
第三个是show动作,显示给定项目的详细信息.此操作是我们在成功更新后重定向到的位置.
流程是:
显示 - >编辑 - >更新(成功:y - >重定向到显示,n - >返回编辑)
我想要实现的是在更新成功时触发标记,以便在下一个Show视图中我可以为用户显示确认消息.问题是我不能100%确定通过RedirectToAction()调用传输数据的最佳方法.有人以为我在使用查询字符串?我们已经在查询字符串中携带变量用于其他目的,但我的一部分对滥用它持怀疑态度.对重定向的调用如下.
RouteValueDictionary dict = Foo.GetRouteValues(bar);
RedirectToAction("Show", dict);
Run Code Online (Sandbox Code Playgroud)
我也读过这个问题,但是如果我没有这个问题,我对使用TempData属性感到很自豪.
谢谢你的一些建议!
我的控制器中有一个用于HttpPost的创建操作.在该操作中,我在db中插入记录,然后返回一个指定不同操作名称的视图,因为我想将用户带到其他地方,例如他们刚创建的记录的详细信息视图,然后我传入当前模型,所以我不必重新加载他们刚刚输入的数据.不幸的是,地址栏中的网址仍然显示原始的创建操作.
[HttpPost]
public ActionResult Create(MyModel model)
{
//Insert record
...
//Go to details view, pass the current model
//instead of re-loading from database
return View("Details", model);
}
Run Code Online (Sandbox Code Playgroud)
如何让网址显示" http:// myapp/MyController/Details/1 ",而不是" http:// myapp/MyController/Create/1 "?是可能的,还是我必须进行重定向?我希望我可以避免重定向......
这是如何在ASP.NET MVC中RedirectToAction的副本,而不会丢失请求数据
嗨,我遇到了一个让我刮伤脑袋的问题.基本上我有一个登录页面Login.aspx,它有用户名和密码字段,以及一个重要的小复选框.登录在AccountController登录方法中处理.目前的代码如下:
[AcceptVerbs(HttpVerbs.Post)]
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings",
Justification =
"Needs to take same parameter type as Controller.Redirect()")]
public ActionResult LogOn(string userName, string password, string returnUrl,
bool sendStoredInfo)
{
if (!this.ValidateLogOn(userName, password)) {
return View();
}
this.FormsAuth.SignIn(userName, false);
if (!String.IsNullOrEmpty(returnUrl)) {
return Redirect(returnUrl);
} else {
return RedirectToAction("Index", "Home");
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,如果该行返回Redirect(returnUrl); 然后它会在另一个控制器OpenIDController中结束,这就是sendStoredInfo bool变得重要的情况.但问题是当我在OpenIDController中时,我没有引用它.如何发送此值?
我设置了搜索路线:
routes.MapRoute(
"Search",
"Search/{q}",
new { controller = "Search", action = "Index" }
);
Run Code Online (Sandbox Code Playgroud)
搜索表单有一个输入框和一个按钮.我希望使用GET进行搜索,如下所示.
<% using(Html.BeginForm("Index", "Search", FormMethod.Get))
{%>
<%:Html.TextBox("q")%>
<span class="query-button">
<input type="submit" value="select" /></span>
<% } %>
</div>
Run Code Online (Sandbox Code Playgroud)
SearchController上的操作是:
public ActionResult Index(string q)
{
// search logic here
return View(new SearchResult(q));
}
Run Code Online (Sandbox Code Playgroud)
URL变成这样: http:// localhost:19502/search?q = mvc + is + great
但我希望搜索结果如下: http:// localhost:19502/search/mvc + is + great
如何设置路由或Html.BeginForm
我想将一个类对象从一个控制器操作传递到不同控制器的操作。
发件人操作
public class CourseController : Controller
{
[HttpPost]
public ActionResult CreateNewCourse(CourseViewModelBase courseViewModel)
{
if (ModelState.IsValid)
{
// Do some stuff
return RedirectToAction("CreateNewProject", "Project",
new { courseVM = courseViewModel});
}
// Bad happened
return View("CreateNewCourse", courseViewModel);
}
Run Code Online (Sandbox Code Playgroud)
接收者行动
public class ProjectController : Controller
{
[HttpGet]
public ActionResult CreateNewProject(CourseViewModelBase courseVM)
{
// Use CourseVM data and do other stuff
return View("Create", projectCreateViewModel);
}
}
Run Code Online (Sandbox Code Playgroud)
我在发送方操作中正确获取数据,并且从重定向到操作调用正确调用接收方操作。但是Receiver Action中的courseVM为null。
我知道这是一个非常古老的问题,并且已经被重复问过。但我发现大多数答案都建议使用TempData并在2008/2009年回答过。我相信会有某种方式使用RedirectToAction without …
在ASP.NET MVC中是否有与Ruby On Rails相同的redirect_to:
提交表格后怎么样?例如:
从page1用户单击指向page2的链接.在第2页上提交表单,然后我想重定向到page1.
如果我将重定向硬编码转到Page1,这很有用.但是,如果用户在第8页上提交表单后点击第8页的第2页链接,我该如何重定向回第8页而不是第1页?
我的问题是当我通过DeleteEmployee中的catch部分时,如何将ModelStateErrors发送到Actionee动作
public ActionResult Employee(int ID, string Name)
{
EmployeeListModel model = new EmployeeListModel (ID, projectName);
return View(model);
}
public ActionResult DeleteEmployee(Employee emp)
{
try
{
emp.Delete();
return RedirectToAction("Employee", new { ID = emp.ID, Name = emp.Name });
}
catch (Exception e)
{
EmployeeListModel model = new EmployeeListModel (emp.ID, emp.Name);
ModelState.AddModelError("Error", e.Message);
return RedirectToAction("Employee", model);
}
}
Run Code Online (Sandbox Code Playgroud)
使用return View("Employee",model); 我仍然无法将ID和Name作为参数发送.