這是我的更新頁面代碼。
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace WebApplication2.Pages.Users
{
public class EditModel : PageModel
{
public UserInfo userInfo = new UserInfo();
public String errorMessage = "";
public String successMessage = "";
public void OnGet()
{
String id=Request.Query["id"];
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", id);
using (SqlDataReader reader = command.ExecuteReader())
{
while (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);
}
}
}
}
}
catch(Exception ex)
{
errorMessage = ex.Message;
}
}
public void OnPost()
{
userInfo.id = Request.Form["id"];
userInfo.name = Request.Form["name"];
userInfo.email = Request.Form["email"];
userInfo.phone = Request.Form["phone"];
userInfo.address = Request.Form["address"];
if (userInfo.id.Length==0 ||userInfo.name.Length == 0 || userInfo.email.Length == 0 || userInfo.phone.Length == 0 || userInfo.address.Length == 0)
{
errorMessage = "All the field are required";
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");
}
}
}
`
這是我的索引頁面(userInfo 類)
索引頁面(userInfo 類)
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Data.SqlClient;
namespace WebApplication2.Pages.Users
{
public class IndexModel : PageModel
{
public List<UserInfo> ListUsers=new List<UserInfo>();
public void OnGet()
{
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";
using (SqlCommand command =new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
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);
}
}
}
}
}
catch(Exception ex)
{
Console.WriteLine("Exception:" ex.ToString());
}
}
}
public class UserInfo
{
public string id;
public string name;
public string email;
public string phone;
public string address;
public string created_at;
}
}
在更新頁面上,我收到了一個錯誤
**將 nvarchar 值 '=2' 轉換為資料型別 int 時轉換失敗。**誰能幫我解決它?我的代碼似乎是正確的。但我有一個錯誤。..................................................................
資料庫結構

但是,關于code readibility,我不太確定您為什么要pattern關注update and display your list. 您甚至可以以最簡單的方式做到這一點,如下保持ADO.NET fashion
另一種實作方式:
全域變數:
List<UserInfo> ListUsers = new List<UserInfo>();
UserInfo userInfo = new UserInfo();
public String errorMessage = "";
public String successMessage = "";
控制器:串列視圖:
public IActionResult Index()
{
string connectionString = "Data Source=WX-6899;Initial Catalog=WsAttendance;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "SELECT * FROM userCaseSO";
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
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);
ListUsers.Add(userInfo);
}
}
}
}
return View(ListUsers);
}
視圖應如下所示:

編輯控制器動作:
public IActionResult Edit(string Id)
{
string connectionString = "Data Source=WX-6899;Initial Catalog=WsAttendance;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "SELECT * FROM userCaseSO WHERE id=@id";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@id", Id);
using (SqlDataReader reader = command.ExecuteReader())
{
while (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);
}
}
}
}
return View(userInfo);
}
編輯視圖:
@model DotNet6MVCWebApp.Models.UserInfo
<div>
<form asp-action="OnPost" asp-controller="AdoDotNetUserUpdate" method="post" enctype="multipart/form-data">
<div>
<h4><strong>Update Info</strong> </h4>
<table class="table table-sm table-bordered table-striped">
<tr>
<th> <label asp-for="id"></label></th>
<td> <input asp-for="id" class="form-control" readonly placeholder="Enter name" /><span asp-validation-for="id"></span></td>
</tr>
<tr>
<th> <label asp-for="name"></label></th>
<td> <input asp-for="name" class="form-control" placeholder="Enter name" /><span asp-validation-for="name"></span></td>
</tr>
<tr>
<th> <label asp-for="email"></label></th>
<td> <input asp-for="email" class="form-control" placeholder="Enter email" /><span asp-validation-for="email"></span></td>
</tr>
<tr>
<th> <label asp-for="phone"></label></th>
<td> <input asp-for="phone" class="form-control" placeholder="Enter phone" /><span asp-validation-for="phone"></span></td>
</tr>
<tr>
<th> <label asp-for="address"></label></th>
<td> <input asp-for="address" class="form-control" placeholder="Enter address" /><span asp-validation-for="address"></span></td>
</tr>
<tr>
<th> <button type="submit" class="btn btn-primary" style="width:107px">Update</button></th>
<td> </td>
</tr>
<tr>
<th>@Html.ActionLink("Back To List", "Index", new { /* id=item.PrimaryKey */ }, new { @class = "btn btn-success" })</th>
<td> </td>
</tr>
</table>
</div>
</form>
</div>
編輯視圖頁面應如下所示:

編輯提交控制器:
[HttpPost]
public IActionResult OnPost()
{
userInfo.id = Request.Form["id"];
userInfo.name = Request.Form["name"];
userInfo.email = Request.Form["email"];
userInfo.phone = Request.Form["phone"];
userInfo.address = Request.Form["address"];
if (userInfo.id.Length == 0 || userInfo.name.Length == 0)
{
errorMessage = "All the field are required";
}
try
{
String connectionString = "Data Source=WX-6899;Initial Catalog=WsAttendance;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
String sql = "UPDATE userCaseSO "
"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 RedirectToAction("Index");
}
最終輸出:

注意:考慮到您的理解,甚至還有優雅的實作方式,目前可以在此級別進行改進。
uj5u.com熱心網友回復:
您收到此錯誤是因為您嘗試將列 DataType 從 String 轉換為 int,這是不正確的。您可以嘗試將IDas Integer轉換為
CAST(@ID AS int)
或者
CONVERT(int,@ID)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/530287.html
