shell
shell

Reputation: 401

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 "[]".

Upvotes: 0

Views: 770

Answers (3)

DineshChauhan
DineshChauhan

Reputation: 121

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

Upvotes: 0

3dd
3dd

Reputation: 2530

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.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

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,                          
             };

Upvotes: 1

Related Questions