【发布时间】:2018-04-09 18:40:57
【问题描述】:
我在 HTML 中有这个输入
<input type="text" placeholder="Username" name="usernameField" id="username" runat="server">
我想获取这个textfield 的值并在cshtml 的c# 方法中使用它,我知道我不能使用常规的document.getElementById。我试过HTML agility pack,但据我所知它不能用于此。这样做的目的是,我有两个输入,用户名和密码,用户在其中输入登录信息,它将输入值与数据库中的值进行比较。
我要使用 HTML 元素值的位置在 SQL 语句中,在用户名和密码处:
WHERE username = " + "'" + username + "'" + " AND password = " + "'" + password
这是完整的c# cshtml 方法:
@using System;
@using Npgsql;
@using HtmlAgilityPack;
@functions{
public Boolean ValidateLogin()
{
NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;User Id=postgres;
Password=postgres;Database=login;");
// Open Database connection.
conn.Open();
// Define a query returning a single row result set.
NpgsqlCommand command = new NpgsqlCommand("SELECT username, password FROM logindata
WHERE username = " + "'" + username + "'" + " AND password = " + "'" + password + "'" + ";", conn);
// Execute the query and obtain a result set.
NpgsqlDataReader dr = command.ExecuteReader();
// If result exists return true and gain access to the control website.
if (dr.Read())
{
return true;
}
// Close Database connection.
conn.Close();
return false;
}
}
更多信息:
- 我在 Visual Studio 2017 中这样做
- 我正在使用 asp.net CORE
- 我正在使用 cshtml 文件(我相信也称为 razor)
【问题讨论】:
-
文本字段是否有 ASP.NET 名称?它在服务器端运行吗?
-
没有asp名称,它只是HTML中的普通文本字段,它在客户端运行。