我有一个名为Shipment的表其中PK的数据类型为uniqueidentifier(Guid在c#)。然后,我有一个用于搜索装运数据的视图页面,结果数据有一个ActionLink这将用户引导到编辑页面,您可以在其中查看和编辑它。问题是,当我单击链接时,只有一个空Guid(0000000 -0000000 -0000000 -0000000 -000000000000)从搜索页面传递到编辑页面。但是,如果我手动复制并粘贴PK到URL,则列将完美地打印到文本框中。谁能帮助我如何正确地通过Guid并在视图页面中读取其相应的数据?
这是Edit的控制器页面。(更新控制器工作正常,所以我没有附加它。
//get shipment info
public ActionResult Edit(Guid id)
{
List<Shipment> shipments = new List<Shipment>();
Shipment shipment = new Shipment();
using (SqlConnection con = new SqlConnection(connectonString))
{
var query = "SELECT * FROM Shipments where sSUID = @SSuid";
//var query = "SELECT * FROM Shipments where sSUID = '" + id + "'";
SqlCommand cmd = new SqlCommand(query, con)
;
cmd.Parameters.Add("@SSuid", SqlDbType.UniqueIdentifier).Value = id;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
shipment.SSuid = (Guid)dr["SSuid"];
//shipment.SSuid = dr.GetGuid(dr.GetOrdinal("SSuid").ToString());
shipment.SFrom = Convert.ToString(dr["SFrom"]);
//shipment.SSuid = Guid.Parse(dr["SSuid"].ToString());
//shipment.SSuid = (Guid).Parse(dr["SSuid"].ToString());
//shipment.SSuid = (Guid)dr["SSuid"].ToString();
shipment.SCompanyName = Convert.ToString(dr["SCompanyName"]);
shipment.SFname = Convert.ToString(dr["SFname"]);
shipment.SMname = Convert.ToString(dr["SMname"]);
shipment.SLname = Convert.ToString(dr["SLname"]);
shipment.SFrom = Convert.ToString(dr["SFrom"]);
shipment.SAddress1 = Convert.ToString(dr["SAddress1"]);
shipment.SAddress2 = Convert.ToString(dr["SAddress2"]);
shipment.SCity = Convert.ToString(dr["SCity"]);
shipment.SState = Convert.ToString(dr["SState"]);
shipment.SPostalCode = Convert.ToString(dr["SPostalCode"]);
shipment.SCountry = Convert.ToString(dr["SCountry"]);
//shipments.Add(shipment);
}
con.Close();
}
return View(shipment);
}
这是搜索的ActionLink部分传递Guid
的页面@Html.ActionLink("Edit", "Edit", new {id=shipment.SSuid}) |
@*@Html.ActionLink("Edit", "Edit", new {id=shipment.SSuid.ToString()})*@
在编辑页面,它在文本框中显示0000000- 0000000- 0000000- 0000000- 000000000000,URL为/Shipment/Edit/0000000 -0000000 -000000000000
@model Receiving.Models.Shipment;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm(new { @class = "form-horizontal" }))
{
@Html.AntiForgeryToken()
;
<div class="form-row">
@Html.HiddenFor(m => m.SSuid)
@Html.TextBoxFor(m => m.SSuid, new { @class = "form-control", tabindex = 1 })
</div>
}
提前谢谢你。
似乎搜索控制器导致错误,因为它为Guid打印了一个空字符串,而结果数据是正确的。这是我的搜索功能。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SearchByTrackingNumber(string? TrackingNumber ) {
List<Shipment> shipments = new List<Shipment>();
using(SqlConnection con = new SqlConnection(connectonString))
{
con.Open();
String sql = "select * from Shipments inner join Packages on Shipments.sSUID = Packages.pSUID where pTrackingNumber like'%" + TrackingNumber + "%' order by sDateReceived";
SqlCommand cmd = new SqlCommand(sql, con);
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Shipment shipment = new Shipment();
//shipment.SSuid = reader.GetGuid(reader.GetOrdinal("SSuid").ToString());
//shipment.SSuid = Guid.Parse(reader["SSuid"]).ToString();
Guid guid = new Guid(reader["SSuid"].ToString());//This is not working
shipment.SCompanyName = Convert.ToString(reader["SCompanyName"]);
shipment.SFname = Convert.ToString(reader["SFname"]);
shipment.SMname = Convert.ToString(reader["SMname"]);
shipment.SLname = Convert.ToString(reader["SLname"]);
shipment.SFrom = Convert.ToString(reader["SFrom"]);
shipment.SAddress1 = Convert.ToString(reader["SAddress1"]);
shipment.SAddress2 = Convert.ToString(reader["SAddress2"]);
shipment.SCity = Convert.ToString(reader["SCity"]);
shipment.SState = Convert.ToString(reader["SState"]);
shipment.SPostalCode = Convert.ToString(reader["SPostalCode"]);
shipment.SCountry = Convert.ToString(reader["SCountry"]);
shipments.Add(shipment);
}
con.Close();
}
}
return View(shipments);
}
我认为这可能是一个问题,因为ASP。. NET MVC不知道它需要抓取的数据在哪里。
可以在Edit函数声明的正上方添加标记[HttpGet]
,如下所示:
[HttpGet]
public ActionResult Edit(Guid id) {
// do stuff
}
话虽如此,我想到的唯一一件事就是为什么会发生这种情况,因为你将suid作为字符串发送,并且在c#和ASP的查询字符串中。. NET不能隐式地将SSuid转换为Guid对象。
所以你可以尝试将参数类型更改为字符串,像这样:public ActionResult Edit(string id) {
// convert id to guid here
// then do stuff
}
请让我知道这是否有效。由于