【问题标题】:mvc 5 validate and redirect to another page on button clickmvc 5 验证并在按钮单击时重定向到另一个页面
【发布时间】:2016-09-27 18:50:08
【问题描述】:

我正在创建一个网络应用程序,我想在按钮单击时重定向页面,我使用所有文本框和按钮作为输入字段我在互联网上看不到太多关于此的内容,如何在按钮单击时重定向到另一个页面(如果验证了 ID 和密码)

例如,我在模态中执行了所有与 sql 相关的查询,并且我从模态 (if true(redirect to login page))(if false(redirect to signup page)) 传递真或假我需要在我的控制器上做什么

    [HttpPost]
    public ActionResult testlogin(string username, string password)
    {
        // called my modal here and creates its object(modal m=new modal();) 
        //what to do now
    }

在我的模式中,我有类似的 sql 查询

`sql command cmd=new sqlcommand("select * from empdet where empid='"+user+"' and pass='"+password+"'",con);
con.open();
SqlDataAdapter da=new SqlDataAdapter(cmd);
dataset ds=new dataset();
da.fill(ds);
if(ds.tables[0].rows.count>0)
{
bool true;
}
else
{
bool false;
}
con.close();
bool = Convert.ToBoolean(cmd.ExecuteReader());
return user;`

如果bool返回true,页面应该重定向到欢迎页面,否则重定向到登录页面

【问题讨论】:

标签: asp.net-mvc


【解决方案1】:

试试这个:-

[HttpPost]
public ActionResult testlogin(string username, string password)
{
    // called my modal here and creates its object(modal m=new modal();) 
    bool loginResult= call your model method
    if(loginResult)
    {
         return RedirectToAction("Welcome","Home");
    }
    else
    {
         return RedirectToAction("Login","Home");
    }
}

【讨论】:

    【解决方案2】:

    使用可以使用

    [HttpPost]
    public ActionResult testlogin(string username, string password)
    {
       return Response.Redirect("../HomeIndex?Option=Please_log_in_as_user");
    }
    

    [HttpPost]
    public ActionResult testlogin(string username, string password)
    {
       return RedirectToAction("AddressDelivery", "BuyOnline", new { Option = "SetAsDeliveryAddress" });
    
    // first one is action, second is controller name, then if needed parameters
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2016-09-09
      • 2019-09-15
      • 1970-01-01
      • 2017-12-06
      • 2017-01-15
      • 2020-01-26
      相关资源
      最近更新 更多