codeNinja
codeNinja

Reputation: 1462

two razor forms on same page. second one gives error

I have two razor forms. I want both to use the same controller. Here is how i am defining the forms:

This works:

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

This does not work:

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

Gives error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

>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

Here is the controller method:

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

Upvotes: 0

Views: 136

Answers (3)

Hamed Moghadasi
Hamed Moghadasi

Reputation: 1673

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

at the end of your controller, you return it to view, but you don't create the view (probably).

check this, please.

Upvotes: 0

Rohit Kumar
Rohit Kumar

Reputation: 1792

In past I have had situations ..where I would have had one razor form and different submit buttons which would hit different actions..

I could achieve this by binding a name attribute to all the different submit buttons. for e.g.

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

Now on the controller side... I would write cases for each type of action

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

So in this way...one razor form can act life multiple razor forms :)

Upvotes: 0

codeNinja
codeNinja

Reputation: 1462

Needed to change the return statement. Since the view does not exist of the same name it should be supplied.

return View("Index", vm);

Upvotes: 1

Related Questions