ASP.NET Core CRUD应用更新页报错:nvarchar转int失败求助
ASP.NET Core CRUD更新操作报错解决:将nvarchar值'=2'转换为int失败
问题描述
开发ASP.NET Core Razor Pages的CRUD应用时,执行用户信息更新操作触发错误:Conversion failed when converting the nvarchar value '=2' to data type int.,数据库users表的id字段为int类型。
错误原因
错误提示中的=2说明表单提交的id值格式异常,大概率是前端Edit页面的id字段(通常为隐藏域)的name属性写错,比如写成name="id=",导致提交的值变成=2而非纯数字;同时后端代码中UserInfo类的id用string类型存储,与数据库int字段不匹配,进一步放大了类型转换失败的概率。
解决步骤
1. 修正前端表单的id字段
检查Edit页面的表单,确保id隐藏域的name属性为id,值为纯数字:
<!-- 推荐使用asp-for标签助手自动绑定 --> <input type="hidden" asp-for="userInfo.id" /> <!-- 手动编写的正确写法 --> <input type="hidden" name="id" value="@Model.userInfo.id" />
避免出现name="id="这类错误写法。
2. 优化后端代码,统一类型匹配
修改UserInfo类的id类型
将id改为int类型,与数据库字段类型保持一致:
public class UserInfo { public int id; public string name; public string email; public string phone; public string address; public string created_at; }
修改EditModel的OnGet方法
加强id参数的校验与类型转换:
public void OnGet() { if (!int.TryParse(Request.Query["id"], out int userId)) { errorMessage = "无效的用户ID"; return; } try { string connectionString = "Data Source=DESKTOP-5406L1M;Initial Catalog=crud;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "SELECT * FROM users WHERE id=@id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@id", userId); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { userInfo.id = reader.GetInt32(0); userInfo.name = reader.GetString(1); userInfo.email = reader.GetString(2); userInfo.phone = reader.GetString(3); userInfo.address = reader.GetString(4); } else { errorMessage = "未找到指定用户"; } } } } } catch(Exception ex) { errorMessage = ex.Message; } }
修改EditModel的OnPost方法
添加id的类型校验,避免无效值传入数据库:
public void OnPost() { if (!int.TryParse(Request.Form["id"], out int userId)) { errorMessage = "无效的用户ID"; return; } userInfo.id = userId; userInfo.name = Request.Form["name"]; userInfo.email = Request.Form["email"]; userInfo.phone = Request.Form["phone"]; userInfo.address = Request.Form["address"]; if (string.IsNullOrEmpty(userInfo.name) || string.IsNullOrEmpty(userInfo.email) || string.IsNullOrEmpty(userInfo.phone) || string.IsNullOrEmpty(userInfo.address)) { errorMessage = "所有字段均为必填项"; return; } try { string connectionString = "Data Source=DESKTOP-5406L1M;Initial Catalog=crud;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql ="UPDATE users " + "SET name=@name, email=@email, phone=@phone, address=@address " + "WHERE id=@id"; using (SqlCommand command = new SqlCommand(sql, connection)) { command.Parameters.AddWithValue("@name", userInfo.name); command.Parameters.AddWithValue("@email", userInfo.email); command.Parameters.AddWithValue("@phone", userInfo.phone); command.Parameters.AddWithValue("@address", userInfo.address); command.Parameters.AddWithValue("@id", userInfo.id); command.ExecuteNonQuery(); } } } catch(Exception ex) { errorMessage=ex.Message; return; } Response.Redirect("/Users/Index"); }
修改IndexModel的代码
适配UserInfo的int类型id:
while (reader.Read()) { UserInfo userInfo = new UserInfo(); userInfo.id = reader.GetInt32(0); userInfo.name = reader.GetString(1); userInfo.email = reader.GetString(2); userInfo.phone = reader.GetString(3); userInfo.address = reader.GetString(4); userInfo.created_at = reader.GetDateTime(5).ToString(); ListUsers.Add(userInfo); }
总结
核心问题是前端表单提交的id值携带多余符号,后端未做严格的类型校验导致转换失败。通过修正前端表单属性、统一类型匹配并加强参数校验,即可解决该错误。
内容的提问来源于stack exchange,提问作者Ruwan Duminda Rathnayaka
相关产品推荐
相关产品推荐

