简体   繁体   中英

How to Read System.GUID on C#

Here's my controller action

[HttpGet]
public JsonResult GetOrderNum(String input)
{
    AEntities db = new AEntities();

    var result = from r in db.orders
                 where r.TrackingNumber.ToString() == input
                 select new {
                     r.Status,                          
                 };

    return Json(result, JsonRequestBehavior.AllowGet);
}

And here I make the AJAX call

var myActionUrl = '@Url.Action("GetTrackingNumber", "ACustomer")';
    var trackingInfo = $('#TrackingNumber').val();

     $('.Track').click(function () {

            $.ajax({
                type: "GET",
                url: myActionUrl,
                data: $('#TrackingNumber').val(),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (json) {
                    alert("Response as JS Object: " + json);
                    console.log(json);
                }
      });

the problem is that it wont read the r.TrackingNumber.ToString() as it is system.guid . Any idea how I can resolve this?

At the moment when I run it, I just get an empty array "[]" .

There are a number of different ways that a GUID can be represented as a string.

System.Guid.ToString() provides an overload with a format specifier . Select a format specifier that matches the format of the GUID found in input .

UPDATE

'System.String ToString()' method, and this method cannot be translated into a store expression

You will need to instead create a GUID from input :

Guid inputGuid = Guid.Parse(input);

var result = from r in db.orders
             where r.TrackingNumber == inputGuid
             select new {
                 r.Status,                          
             };

Change the method public JsonResult GetOrderNum(String input) to read public JsonResult GetOrderNum(Guid input) thus passing the parameter as a GUID. Then there is no need to convert the data store GUID.

       function FunctionName(Input) {  
          $.ajax({
            type: 'GET',
            url: '/GetOrderNum?Input=' + Input=', // Your URL Here
           data: $('#TrackingNumber').val(),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (json) {
                alert("Response as JS Object: " + json);
                console.log(json);
            }
      });
 }

  [HttpGet]
   public JsonResult GetOrderNum()
   {

      string input =HttpContext.Current.Request["Input"].ToString();              
      Guid input= Guid.Parse(input);
      AEntities db = new AEntities();

             var result = from r in db.orders
             where r.TrackingNumber.ToString() == input
             select new {
                 r.Status,                          
             };

          return Json(result, JsonRequestBehavior.AllowGet);
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM