【问题标题】:two razor forms on same page. second one gives error同一页上的两个剃须刀形式。第二个给出错误
【发布时间】:2017-10-28 23:32:24
【问题描述】:

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

这行得通:

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

【问题讨论】:

  • 向你展示控制器方法(你试图返回一个不存在的视图)
  • 好像有ControllerAction,但是Views-Folder中没有resetPassword.cshtml。

标签: c# asp.net-mvc razor


【解决方案1】:

这个错误说:“嘿,我搜索视图文件夹,但我找不到 那里有任何类型的“重置密码”。 "

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

请检查一下。

【讨论】:

    【解决方案2】:

    需要更改返回语句。由于视图不存在同名的视图,因此应提供它。

    返回视图("索引", vm);

    【讨论】:

      【解决方案3】:

      过去我曾遇到过这样的情况……我会使用一种剃须刀表单和不同的提交按钮来执行不同的操作……

      我可以通过将名称属性绑定到所有不同的提交按钮来实现这一点。 例如

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

      所以这样...一个剃须刀形态可以生活多个剃刀形态:)

      【讨论】:

        猜你喜欢
        • 2020-05-30
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 2021-09-22
        • 2021-04-05
        • 1970-01-01
        • 2016-10-15
        • 2021-11-28
        相关资源
        最近更新 更多