Redirect to another page when a button is clicked

I am creating a web application and I have 2 text fields and one button with input field

<input id="Login" type="submit" onclick="location.href='@Url.Action("loggedin","logincontroller")'" style="" width="200" height="34" value="submit" />

and my controller looks like

public ActionResult loggedin(string role, string username, string password)
{
    webservice.loginservice a= new webservice.loginservice();
    a.getlogintype(role, username, password);
    return View();
}

with my web service

[WebMethod]
public string getlogintype(string role, string username, string password)
{
    string strtru="";
    string strfalse="";
    sqlcon;
    SqlCommand cmd = new SqlCommand("select * from [admin] where userid='" + username + "' and pass ='" + password + "'", con);
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        strtru = "true";

    }
    else
    {
        strfalse = "false";

    }
    con.Close();
}

My controller calls a web service, and now I want to know how to redirect the page to a successful check. This is my first application using mvc. I know that there are a lot of errors in this application (please let me know all the errors).

I am creating an application on mvc 5 and I use only the input fields, and not @Htmlbecause I want to create it ( beta2.hti-india.com ) this application was created by me in asp.net C #.

+4
source share
2 answers

your web service code looks good.

changes to your code.

public ActionResult loggedin(string role, string username, string password)
{
 webservice.loginservice a= new webservice.loginservice();
 string result = a.getlogintype(role, username, password);
 if(result == "true")
 {
   // redirect to your application home page or other page
   return RedirectToAction("OtherAction", "Controller");
 }
 else
 {
   return View();
 }  
}

.cshtml

@using (Html.BeginForm("loggedin", "controller", new { ReturnUrl = "" }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{

<!-- your textbox here-->

    <input id="Login" type="submit" value="submit">

}
0

loggedin (..) , RedirectToAction()

return RedirectToAction("MyAction", "MyController");
0

Source: https://habr.com/ru/post/1655828/


All Articles