Search code examples
asp.net-mvcado.netrazor-pages

Guid shows empty string in view page (ASP.NET MVC)


I have a table called Shipment where its data type of PK is uniqueidentifier (Guid in C#).Then I have a View page for searching the Shipment data and the result data has an ActionLink that directs the user to the edit page where you can view and edit it. The problem is, when I click the link, only an empty Guid (00000000-0000-0000-0000-000000000000) is passed from the search page to the edit page. However, if I manually copy and paste the PK to URL, the columns are printed perfectly into the textboxes. Can anyone help me with how to properly pass the Guid and read its corresponding data in a View page?

Here is the controller for Edit page. (The update controller is working just fine so I am not attaching it.

//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);

    }

Here is the ActionLink part of the Search page that passes the Guid

@Html.ActionLink("Edit", "Edit", new {id=shipment.SSuid}) |
@*@Html.ActionLink("Edit", "Edit", new {id=shipment.SSuid.ToString()})*@ 

In the Edit page,it says 00000000-0000-0000-0000-000000000000 in the textbox and in URL as /Shipment/Edit/00000000-0000-0000-0000-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>
}

Thank you in advance.

UPDATED It seems like Search controller is causing the error since it prints an empty string for Guid while the result data is correct. Here is my search function.

[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); 
        }

Solution

  • I think this might be a problem because ASP.NET MVC doesn't know where the data it needs to grab is.

    You might add the tag [HttpGet] right above the function declaration of Edit, like this:

    [HttpGet]
    public ActionResult Edit(Guid id) {
       // do stuff
    }
    

    That being said, the only other thing that comes to my mind on why this would be happening is because you send the SSuid as a string, and in the query string C# and ASP.NET cannot implicitly convert the SSuid to a Guid object.

    So you could try changing the parameter type to string like this:

    public ActionResult Edit(string id) {
       // convert id to guid here
       // then do stuff
    }
    

    Please let me know if this works. Thanks