两个剃刀形式在同一页面上。第二个给出错误

时间:2017-10-28 23:32:24

标签: c# asp.net-mvc razor

我有两种剃须刀形式。我希望两者都使用相同的控制器。以下是我如何定义表单:

这有效:

 @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 info@example.com for help.";
            }
        } 

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

3 个答案:

答案 0 :(得分:1)

需要更改return语句。由于视图不存在同名,因此应该提供。

返回视图(&#34;索引&#34;,vm);

答案 1 :(得分:1)

  

此错误说:&#34;嘿Dud我搜索Views文件夹,但我找不到   任何类型的ResetPassword&#39;那里。 &#34;

在控制器的末尾,您将其返回查看,但您不会创建视图(可能)。

请检查一下。

答案 2 :(得分:0)

过去我曾经遇到过这样的情况......我会有一个剃刀形式和不同的提交按钮,这些按钮会有不同的动作。

我可以通过将name属性绑定到所有不同的提交按钮来实现此目的。 例如

<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);
   }
}

所以这样......一个剃刀形式可以扮演多种剃刀形式:)