通过查询字符串传递 Int 类型值
Passing Int type value via query string
我能够成功传递字符串值,但无法通过查询字符串传递整数值。
如果我只传递字符串对象,URL 看起来像
www.website.com/mypage?ProductName=TestName&MahName=TestName
(正确)
但是,如果我将 Id (int)
与查询字符串一起传递,URL 看起来像
www.website.com/mypage/1?ProductName=TestName&MahName=TestName
(不正确)
不过,我希望它是
www.website.com/mypage?Id=1&ProductName=TestName&MahName=TestName
型号
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;
namespace SecureMedi.Models
{
public class CustomProductData
{
[Required]
public int Id { get; set; }
[StringLength(200)]
public string ProductName { get; set; }
[StringLength(100)]
public string MahName { get; set; }
public static CustomProductData FromSqlReader(SqlDataReader rdr)
{
return new CustomProductData
{
Id = (int)rdr["id"],
ProductName = rdr["product_name"].ToString(),
MahName = rdr["mah_name"].ToString()
};
}
}
}
控制器
[HttpGet]
public ActionResult CustomProductData(int Id, string ProductName, string MahName) {
var model = new CustomProductData() {
Id = Id,
ProductName = ProductName,
MahName = MahName
};
return View(model);
}
[HttpPost]
public ActionResult CustomProductData(CustomProductData cp) {
try {
using(ISecureMediDatabase db = new SecureMediDatabase(this)) {
CustomProductDataDAL cpd = new CustomProductDataDAL(db);
cpd.Edit(cp);
return RedirectToAction("LoadCustomProductData");
}
} catch (Exception ex) {
ModelState.AddModelError("", ex.Message);
return View(cp);
}
}
达尔
public void Edit(CustomProductData cp) {
try {
string sql = "UPDATE table_name SET product_name = @ProductName, mah_name = @MahName WHERE id = @Id";
if (cp.HasDetails()) {
using(SqlCommand cmd = new SqlCommand(sql, conn)) {
cmd.Parameters.Add(new SqlParameter("@Id", cp.Id));
cmd.Parameters.Add(new SqlParameter("@ProductName", cp.ProductName));
cmd.Parameters.Add(new SqlParameter("@MahName", cp.MahName));
PrepareCommand(cmd);
cmd.ExecuteNonQuery();
}
}
} catch {
closeConnection();
throw;
}
}
cshtml
锚点 link 将查询字符串值传递到编辑页面(从 QueryString 获取值)
<td class="text-secondary">@Html.ActionLink("Edit", "CustomProductData", "Home", new { Id = @item.Id, ProductName = @item.ProductName, MahName = @item.MahName }, new { @class = "text-info" })</td>
显然,变量名Id
在路由中被占用,因此将变量名Id
更改为RecordId
解决了问题。
我能够成功传递字符串值,但无法通过查询字符串传递整数值。
如果我只传递字符串对象,URL 看起来像
www.website.com/mypage?ProductName=TestName&MahName=TestName
(正确)
但是,如果我将 Id (int)
与查询字符串一起传递,URL 看起来像
www.website.com/mypage/1?ProductName=TestName&MahName=TestName
(不正确)
不过,我希望它是
www.website.com/mypage?Id=1&ProductName=TestName&MahName=TestName
型号
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;
namespace SecureMedi.Models
{
public class CustomProductData
{
[Required]
public int Id { get; set; }
[StringLength(200)]
public string ProductName { get; set; }
[StringLength(100)]
public string MahName { get; set; }
public static CustomProductData FromSqlReader(SqlDataReader rdr)
{
return new CustomProductData
{
Id = (int)rdr["id"],
ProductName = rdr["product_name"].ToString(),
MahName = rdr["mah_name"].ToString()
};
}
}
}
控制器
[HttpGet]
public ActionResult CustomProductData(int Id, string ProductName, string MahName) {
var model = new CustomProductData() {
Id = Id,
ProductName = ProductName,
MahName = MahName
};
return View(model);
}
[HttpPost]
public ActionResult CustomProductData(CustomProductData cp) {
try {
using(ISecureMediDatabase db = new SecureMediDatabase(this)) {
CustomProductDataDAL cpd = new CustomProductDataDAL(db);
cpd.Edit(cp);
return RedirectToAction("LoadCustomProductData");
}
} catch (Exception ex) {
ModelState.AddModelError("", ex.Message);
return View(cp);
}
}
达尔
public void Edit(CustomProductData cp) {
try {
string sql = "UPDATE table_name SET product_name = @ProductName, mah_name = @MahName WHERE id = @Id";
if (cp.HasDetails()) {
using(SqlCommand cmd = new SqlCommand(sql, conn)) {
cmd.Parameters.Add(new SqlParameter("@Id", cp.Id));
cmd.Parameters.Add(new SqlParameter("@ProductName", cp.ProductName));
cmd.Parameters.Add(new SqlParameter("@MahName", cp.MahName));
PrepareCommand(cmd);
cmd.ExecuteNonQuery();
}
}
} catch {
closeConnection();
throw;
}
}
cshtml
锚点 link 将查询字符串值传递到编辑页面(从 QueryString 获取值)
<td class="text-secondary">@Html.ActionLink("Edit", "CustomProductData", "Home", new { Id = @item.Id, ProductName = @item.ProductName, MahName = @item.MahName }, new { @class = "text-info" })</td>
显然,变量名Id
在路由中被占用,因此将变量名Id
更改为RecordId
解决了问题。