Asp.net 检查对象列表是否为true if is redirect if not抛出错误警告
嗨,我需要检查控制器中的登录名和密码是否正确,如果是,我想重定向到索引页,如果不在浏览器中抛出错误警告。但问题是我不知道如何将对象列表转换为布尔值。而且我也不知道如何通过如下控制器更改按钮的文本:Asp.net 检查对象列表是否为true if is redirect if not抛出错误警告,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,嗨,我需要检查控制器中的登录名和密码是否正确,如果是,我想重定向到索引页,如果不在浏览器中抛出错误警告。但问题是我不知道如何将对象列表转换为布尔值。而且我也不知道如何通过如下控制器更改按钮的文本:button1id.text=“用户或密码处于正确的未找到状态”它给了我一个错误。你能帮我吗 如果它给了我这个错误: 错误1无法将类型“System.Collections.Generic.List”隐式转换为“bool” 在else中,我犯了一个错误: 错误2当前上下文中不存在名称“button1id
button1id.text=“用户或密码处于正确的未找到状态”代码>它给了我一个错误。你能帮我吗
如果它给了我这个错误:
错误1无法将类型“System.Collections.Generic.List”隐式转换为“bool”
在else中,我犯了一个错误:
错误2当前上下文中不存在名称“button1id”
登录控制器:
[HttpGet]
public ActionResult Login()
{
return View("Login", "_LayoutLogin");
}
[HttpPost]
public ActionResult Login(string uzivatel, string heslo)
{
List<Login> list = LoginServiceLayer.Instance.SelectByJmenoHeslo(uzivatel, heslo);
if (list)
{
Response.Redirect("Index");
}
else
{
button1id.Text = "User or password is in correct not found";
}
}
[HttpGet]
公共操作结果登录()
{
返回视图(“登录”,“布局登录”);
}
[HttpPost]
公共操作结果登录(字符串uzivatel、字符串heslo)
{
List List=LoginServiceLayer.Instance.SelectByJmenoHeslo(uzivatel,heslo);
如果(列表)
{
响应。重定向(“索引”);
}
其他的
{
button1id.Text=“未找到正确的用户或密码”;
}
}
我在控制器中使用的数据库层的函数:
public List<Login> SelectByJmenoHeslo(string jmeno, string heslo)
{
string passwords = e.encryption(heslo);
string queryString = "SELECT jmeno, heslo from Login where (jmeno = '" + @jmeno + "') AND (heslo = '" + @passwords + "');";
SqlCommand command = new SqlCommand(queryString, Connection);
command.Parameters.AddWithValue("@jmeno", "");
command.Parameters.AddWithValue("@passwords", "");
try
{
SqlDataReader reader = command.ExecuteReader();
List<Login> login = new List<Login>();
while (reader.Read())
{
Login l = new Login();
l.jmeno = reader[0].ToString();
l.heslo = reader[1].ToString();
Console.WriteLine("jmeno: " + l.jmeno + " " + "heslo: " + " " + " " + l.heslo);
login.Add(l);
}
reader.Close();
return login;
}
catch (Exception ex)
{
chyba.zapsat_do_souboru(ex.Message);
Console.OpenStandardOutput();
Console.WriteLine(ex);
return null;
}
}
public List SelectByJmenoHeslo(字符串jmeno,字符串heslo)
{
字符串密码=加密(heslo);
string queryString=“从登录位置(jmeno=”+@jmeno+”)和(heslo=”+@passwords+”)选择jmeno,heslo;”;
SqlCommand=newsqlcommand(queryString,Connection);
command.Parameters.AddWithValue(“@jmeno”,”);
command.Parameters.AddWithValue(“@passwords”,”);
尝试
{
SqlDataReader=command.ExecuteReader();
列表登录=新建列表();
while(reader.Read())
{
Login l=新登录名();
l、 jmeno=reader[0]。ToString();
l、 heslo=reader[1]。ToString();
Console.WriteLine(“jmeno:+l.jmeno++++”heslo:+l.heslo);
login.Add(l);
}
reader.Close();
返回登录;
}
捕获(例外情况除外)
{
chyba.zapsat_do_souboru(例如信息);
Console.OpenStandardOutput();
控制台写入线(ex);
返回null;
}
}
和视图:
@using (Html.BeginForm("Login", "Login", FormMethod.Post, new { @area = "Home", @role = "form" }))
{
<input id="textinput" name="jmeno" type="text" placeholder="login" class="form-control input-md" required="">
<input id="textinput" name="heslo" type="text" placeholder="heslo" class="form-control input-md" required="">
<a href="@Url.Action("Login", "Login")">
<input type="submit" id="button1id" value="Pokračovat" name="button1id" class="btn btn-success">
</a>
}
@使用(Html.BeginForm(“Login”、“Login”、FormMethod.Post、new{@area=“Home”、@role=“form”}))
{
}
我不想显得严厉,但你知道你在做什么吗?Asp.NETMVC中没有按钮,因为视图(MVC的V)没有用户控件的概念。你似乎不明白你调用的方法会给你什么
AFAICT,如果该方法返回一行,您可以说登录正常,因此您可以更改您的行:
if (list)
到
但是,您仍然没有处理身份验证:在登录页面之后,应该以某种方式(例如,使用cookie)向服务器“了解”用户。
因此,您还需要:
FormsAuthentication.SetAuthCookie(list[0]);
{
ViewBag.ErrorMessage = "Wrong username or password";
return View();
}
如果,即您正在使用表单身份验证。
请看这里:
在其他方面,您需要以下内容:
FormsAuthentication.SetAuthCookie(list[0]);
{
ViewBag.ErrorMessage = "Wrong username or password";
return View();
}
您需要在视图上打印一个标签,以显示错误消息
简单地说,你的问题太宽泛了,唯一正确的答案是:找一本关于Asp.NETMVC的好书(可能还有另一本关于C#或一般编程的书),从头读到尾
编辑:我没有特别注意您的数据库代码。这在很多方面都是错误的,我的眼睛在流血。说真的,请拿本书来