2017-10-28 64 views
0

我有两个剃刀形式。我希望两者都使用相同的控制器。这里是我如何定义形式:两张剃须刀形式在同一页上。第二个给出错误

这工作:

@using (Html.BeginForm("Index", "Login", FormMethod.Post, new { enctype = "multipart/form-data" })) 

这不起作用:

@using (Html.BeginForm("ResetPassword", "Login", FormMethod.Post, new { enctype = "multipart/form-data" })) 

给出错误:

说明:在出现未处理的异常执行当前的Web请求。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。

>Exception Details: System.InvalidOperationException: The view 'ResetPassword' or its master was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Login/ResetPassword.aspx 
~/Views/Login/ResetPassword.ascx 
~/Views/Shared/ResetPassword.aspx 
~/Views/Shared/ResetPassword.ascx 
~/Views/Login/ResetPassword.cshtml 
~/Views/Login/ResetPassword.vbhtml 
~/Views/Shared/ResetPassword.cshtml 
~/Views/Shared/ResetPassword.vbhtml 

这里是控制器方法:

[HttpPost] 
public ActionResult ResetPassword(LoginViewModel vm) 
{ 
    try 
    { 
     ViewBag.ErrorMsg = ""; 
     if (vm.confirmpass != vm.newPass) 
     { 
      ViewBag.ErrorMsg = "Passwords do not match."; 
     } else if (!String.IsNullOrWhiteSpace(vm.user) && !String.IsNullOrWhiteSpace(vm.newPass) && !String.IsNullOrWhiteSpace(vm.confirmpass)) 
     { 
      //this should be updated to be empty string once the database is setup 
      string sysproId = "1234"; 
      sysproId = ""; 

      //get from the database 
      string constr = ConfigurationManager.ConnectionStrings["mySQLConnStr"].ConnectionString; 
      using (MySqlConnection con = new MySqlConnection(constr)) 
      { 
       string query = "SELECT * from wp_portal_users where username='" 
        + vm.user + "' and ((tempPassword='" + vm.newPass + "' and NOW()<= tempPasswordValidity));"; 
       using (MySqlCommand cmd = new MySqlCommand(query)) 
       { 
        cmd.Connection = con; 
        con.Open(); 
        using (MySqlDataReader sdr = cmd.ExecuteReader()) 
        { 
         while (sdr.Read()) 
         { 
          sysproId = sdr["sysproID"].ToString(); 
         } 
        } 
       } 

       //if a user is found then update the password 
       if (!String.IsNullOrWhiteSpace(sysproId)) { 
        query = "Update wp_portal_users set password='" + vm.newPass + "' where username='" + vm.user + "'"; 
        using (MySqlCommand cmd2 = new MySqlCommand(query)) 
        { 
         cmd2.Connection = con; 
         con.Open(); 
         cmd2.ExecuteNonQuery(); 
        } 
       } 

       //close the db connection 
       con.Close(); 
      } 



      //log the user in if there was a match 
      if (!String.IsNullOrWhiteSpace(sysproId)) 
      { 
       //store the users details in the cookie 
       HttpCookie userInfo = new HttpCookie("123Cookie"); 
       userInfo["Userid"] = "my_portal";//this is the userID of the site and not the user 
       userInfo["CustomerId"] = sysproId; 

       //cookie expires everyday 
       userInfo.Expires.Add(new TimeSpan(0, 1, 0)); 
       Response.Cookies.Add(userInfo); 

       Session["sysproId"] = sysproId; 
       return RedirectToAction("Index", "Home"); 
      } 
      else 
      { 
       //user was not found. Show some error 
       vm.user = ""; 
       vm.pass = ""; 
       ViewBag.ErrorMsg = "Could not login. Please email us at [email protected] for help."; 
      } 
     } 

     return View(vm); 
    } 
    catch (Exception ex) 
    { 
     ViewBag.ErrorMsg = "Whoops! Please try again."; 
     return View(vm); 
    } 
} 
+0

显示你的控制器方法(你试图返回一个不存在的视图) –

+1

看来,有一个ControllerAction,但在Views-Folder中没有resetPassword.cshtml。 – Nikolaus

回答

1

需要改变return语句。由于视图不存在相同的名称,因此应该提供它。

return View(“Index”,vm);

0

在过去,我已经有一个名称属性绑定到各种不同的提交按钮..where我将不得不一个剃刀形式和不同的提交按钮这将打不同的操作情况..

我能做到这一点。例如对于例如

<button type="submit" name="submitBtn" value="Login" >Login</button> 
<button type="submit" name="submitBtn" value="Reset" >ResetPassword</button> 

现在遥控器侧...我会写的情况下以这种方式每种类型的动作

public ActionResult Index(LoginViewModel vm , string submitBtn) 
{ 
    if(submitBtn == "Login") 
    { 
    //do the login thing & 
     return View("Index", vm); 
    } 
    if(submitBtn == "Reset") 
    { 
    // do the reset password thing and 
    return View("Index", vm); 
    } 
} 

所以......一个剃须刀形式可以充当多种生活形态的剃须刀:)

0

this Error says that: "Hey Dud I search Views folder, but I can't find any kind of 'ResetPassword' there. "

在控制器结束时,您将其返回至视图,但不创建视图(可能)。

请检查此。