重定向URL表单提交后 [英] Redirect Url After Form Submission

查看:154
本文介绍了重定向URL表单提交后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本形式,它允许用户创建一个员工。一旦用户选择了提交按钮,页面应该是重定向来一个摘要页面。本摘要页面需要显示员工表格上添加的细节。

例如,我有一个看起来像这样的形式:

在这里输入的形象描述

一旦用户选择提交,它应该然后将用户重定向到以下页面:

在这里输入的形象描述


  

这是我遇到问题越来越即重定向URL 我的URL看起来像:的 http://www.localhost.com/Employee/1234/ 1234 重新presents的员工 ID。我怎样才能扳指员工标识


我的当前HTML

 <按钮式=提交名称=saveButtonID =saveBtn级=BTN BTN-小学>提交< /按钮>


  

请注意,我用的.NET,所以很愿意使用剃刀语法。


这是故事的更新

阅读所有的意见后,我去更深入地挖掘我的问题。这是我发现的。

有与下列路线 RouteConfig.cs 文件:

  routes.MapRoute(
  名称:ListEmployee
  网址:员工/ {}雇员,
  默认:新{控制器=雇员,行动=名单},
  限制:新{雇员=\\\\ D +}

由于我有这条路,我决定修改该控制器的方法来利用它:

  [HttpPost]
公众的ActionResult AddEmployee(视图模型< EmployeeModel>模型)
{
  //这里的逻辑  返回RedirectToAction(ListEmployee,新的{雇员= model.Body.Employee.Id,fromEmployeeProject =真});
}


  

问题:这完全崩溃。说明:在路由表中没有路由匹配所提供的值什么我做错了任何想法



解决方案

如果您正在使用实体框架(EF),它遵循了<$每个插入语句C $ C> SCOPE_IDENTITY()当使用自动生成的ID。


  

SCOPE_IDENTITY返回插入到相同范围的标识列的最后一个标识值。 ( MSDN


因此​​,如果你的员工 ID是在数据库中自动生成的列,你应该保存更改后的权利能够得到的ID:

  [HttpPost]
公众的ActionResult添加(员工员工)
{
    使用(VAR上下文=新MyContext())
    {
        //添加Employee对象
        context.Employees.Add(员工);
        // 保存更改
        context.SaveChanges();
        //该ID将可在这里
        INT雇员= employee.Id;
        // ..所以执行您重定向到相应的动作,_
        //并通过employee.Id作为参数传递给动作
        返回RedirectToAction(指数,雇员,新{ID =雇员});
    }
}

显然,你有没有为我们提供您的控制器和动作的名称,以便确保您用适当的值替换它们。

I have a basic form that allows a user to create an Employee. Once the user selects the submit button, the page should be redirected to a summary page. This summary page needs to show the details that were added on the Employee form.

For example, I have a form that looks like this:

Once the user selects Submit, it should then redirect the user to the following page:

The issue that I'm running into is getting that redirect url. My url looks something like: http://www.localhost.com/Employee/1234/ The 1234 represents the Employee id. How can I pull that Employee Id?

My Current HTML

<button type="submit" name="saveButton" id="saveBtn" class="btn btn-primary">Submit</button>

Please note that I am using .NET, so am open to using Razor Syntax.

Update on Story

After reading all of your comments, I went and dug deeper into my problem. This is what I found.

There is a RouteConfig.cs file with the following route:

routes.MapRoute(
  name: "ListEmployee",
  url: "Employee/{employeeId}",
  defaults: new { controller = "Employee", action = "List"},
  constraints: new {employeeId = "\\d+"}
)

Because I have this route, I decided to modify this controller method to utilize it:

[HttpPost]
public ActionResult AddEmployee(ViewModel<EmployeeModel> model)
{
  // logic here

  return RedirectToAction("ListEmployee", new { employeeId = model.Body.Employee.Id, fromEmployeeProject = true });
}

Issue: This completely crashes. Stating: No route in the route table matches the supplied values. Any ideas on what I am doing wrong?

解决方案

If you're using Entity Framework (EF), it follows up each INSERT statement with SCOPE_IDENTITY() when auto-generated ids are used.

SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. (MSDN)

As such, if your Employee ID is an auto-generated column in the database, you should be able to get the ID right after saving your changes:

[HttpPost]
public ActionResult Add(Employee employee)
{
    using (var context = new MyContext())
    {
        // add the employee object
        context.Employees.Add(employee);
        // save changes
        context.SaveChanges();
        // the id will be available here
        int employeeId = employee.Id;   
        // ..so perform your Redirect to the appropriate action, _
        // and pass employee.Id as a parameter to the action       
        return RedirectToAction("Index", "Employee", new { id = employeeId });            
    }
}

Obviously you haven't provided us with your controller and action names so make sure that you replace them with the appropriate values.

这篇关于重定向URL表单提交后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆