user21031522
user21031522

Reputation:

How to return JSON result in controller

I would like to return JSON result in my controller.

In my controller, I am getting an array of GUID and passing that to the CheckRecords method.

Currently, I am not sure how to get the data back from the data service layer to my controller.

Here is my full code

Controller:

public JsonResult CheckData()
{
    string guid = HttpContext.Request["GUID"];
    string result = Services.CheckRecords( guid);
    return Json(result);
}

Service Layer:

public static IdIdentity CheckRecords(string guid)
{           
    return DataServices.Services.CheckRecords(guid);
}

Interface:

public interface IServices
{
    IdIdentity CheckRecords(string guid);
}

Data service layer where it returns a list:

public class Services : IServices
{
    public const string PARAM_FORM_GUID = "guid";
     
    public List<IdIdentity> CheckRecords(string guid)
    {
        IdIdentity Code = new IdIdentity();

        if (Code == null)
            return null;            
            
        string sql= "SELECT Name FROM OCODE" +            
            "  WHERE SID IN(SELECT SID FROM OITEMS WHERE OITEM_ID IN (" + PARAM_FORM_GUID + "))";
            

        var parameters = new List<DbParameter>
        {
            CreateParameter(PARAM_FORM_GUID, DbType.String, guid, DBNull.Value),
        };

        sql = ReplaceParameterPrefix(sql);

        return ReadAll(sql, parameters, reader =>
        {
            return new IdIdentity(reader.ReadByName(T_EXCLUSIONS.EXCLUDED_ID, string.Empty),
                reader.ReadByName(T_EXCLUSIONS.EXCLUDED_ID_SCH, string.Empty));
        }).ToList();
    }
}

ReplaceParameterPrefix function:

public static string ReplaceParameterPrefix(string sqlString)
{
    if (DataServices.TypeOfProvider == DataServices.ProviderType.SqlServer)
    {
        sqlString = sqlString.Replace(':', '@');
    }

    return sqlString;
}

Model:

[Serializable]
public class IdIdentity : GenericType
{
    public string Id { get; set; }
    public string Scheme { get; set; }    
    public string Name { get; set; }
        
    public

    public IdIdentity(string id, string scheme, string name = null)
    {
        Id = id;
        Scheme = scheme;
        Name = name;    
    }
}

Please suggest to me how to get the list of result and return it as JSON in my controller.

Upvotes: 0

Views: 88

Answers (1)

Yong Shun
Yong Shun

Reputation: 51160

Issue & Concern

  1. Not implement the method from IService.

From your IService

IdIdentity CheckRecords(string guid);

and

public class Services : IServices
{
    public const string PARAM_FORM_GUID = "guid";
     
    public List<IdIdentity> CheckRecords(string guid) { ... }
}

You didn't implement the method from IService in the Services class.

  1. Compilation error for assigning the IdIdentity value to the variable of string type.
string result = Services.CheckRecords( guid);

Solution

  1. Modify the CheckRecords method signature to return the List<IdIdentity> type in the IServices interface.
public interface IServices
{
    List<IdIdentity> CheckRecords(string guid);
}
  1. The change in 1 requires you to modify CheckRecords method signature to return List<IdIdentity> type in Service Layer.

Service Layer

public static List<IdIdentity> CheckRecords(string guid)
{           
    return DataServices.Services.CheckRecords(guid);
}
  1. You should use implicit-type variable (var) or explicit-type variable (List<IdIdentity>) for the result variable.
public JsonResult CheckData()
{
    string guid = HttpContext.Request["GUID"];
    var result = Services.CheckRecords( guid);  // Implicit-typed
    // Or
    // List<IdIdentity> result = Services.CheckRecords( guid); // Explicit-typed
    return Json(result);
}

Upvotes: 1

Related Questions