2017-10-29 49 views
0

我有一个剃刀形式。这是表单的有用部分。对表单提交失败url正在改变

<div id="forgotBox" style="display:none;" class="col-xs-12 col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 centerit"> 
<div class="col-md-12"> 


    @using (Html.BeginForm("ForgotPassword", "Login", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

     <div class="form-group"> 
      @Html.EditorFor(x => x.user, new { htmlAttributes = new { @class = "form-control", placeholder = "Email" } })<br /> 
      @Html.ValidationMessageFor(x => x.user) 
     </div> 

     if (!String.IsNullOrWhiteSpace(ViewBag.ErrorMsg)) 
     { 
      <div class="form-group field-validation-error"> 
       @ViewBag.ErrorMsg 
      </div> 
     } 
     <div class="form-group"> 
      <input type="submit" class="btn btn-primary" /> 
     </div> 
    } 

    <div><a href="javascript:showLogin();">Login</a></div> 
</div> 

这里是它调用控制器方法:

[HttpPost] 
    public ActionResult ResetPassword(LoginViewModel vm) 
    { 
      ViewBag.ErrorMsg = ""; 
      if (vm.confirmpass != vm.newPass) 
      { 
       //some error condition 
       ViewBag.ErrorMsg = "Passwords do not match."; 
      } else if (!String.IsNullOrWhiteSpace(vm.user)){ 
       //code here for when you want to process when we have the userID 
      } 


      return View("ForgotPassword", vm); 
     } 

    } 
} 

URL当用户到达这个页面example.org/login?user=123

return View("ForgotPassword", vm);的URL更改后

example.org/login/ForgotPassword 

我希望它保持example.org/login?user=123,因为页面未成功提交并且正在使用URL参数。

回答

0

您可以将用户的querystring值作为路由值传递。

@using (Html.BeginForm("ResetPassword", "Login", new { user = Request["user"] })) 
{ 
} 

当剃须刀执行此代码时,它将渲染带查询字符串的窗体标签,其中包含用户键和值。

<form action="/Login/ResetPassword?user=123" method="post"> 

</form> 

另一种选择是设置你的视图模型的用户属性的GET操作方法和助手将生成设置为该值值的输入元素,当你提交后,当你的代码返回到视图,价值将在那里。 (你不会在查询字符串中,但它会在请求正文中提交表单时。

+0

你是在说我摆脱了'new {enctype =“multipart/form-data”})) ' – codeNinja

+0

你为什么需要这个?当你想发送一个文件(文件上传)时你需要这个属性, – Shyju