Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I am using MVC3 in my application. In my controller i have to return Json data to bind my JQGrid.

Based on a condition, i want to build my Json data. For this i need to declare the variable
"var jsonData" (refer below code) and initialize it to some value initially, so that i can build the query and return it at last to bind the JQGrid.

I tried to initialize it in many ways like by giving empty string or assigning it to null etc. But i failed,please tell me How can i initialize it to null?

public JsonResult LoadByName(string txtSearchVal, string SearchBy) 
{ 

//var jsonData= new list<string>; 
if (SearchBy.ToUpper() == "FIRSTNAME") 
{ 
var jsonData = new 
{ 
records = tskgrp.UserDetails.Count(), 
rows = ( 
from user in tskgrp.UserDetails 
where (user.FirstName.StartsWith(txtSearchVal)) 
select new 
{ 
i = user.ID, 
cell = new string[] { user.ID.ToString(), user.FirstName, user.LastName, user.Active.ToString() } 
}).ToArray() 
}; 
} 
if (SearchBy.ToUpper() == "LASTNAME") 
{ 
var jsonData = new 
{ 
records = tskgrp.UserDetails.Count(), 
rows = ( 
from user in tskgrp.UserDetails 
where (user.FirstName.Contains(txtSearchVal)) 
select new 
{ 
i = user.ID, 
cell = new string[] { user.ID.ToString(), user.FirstName, user.LastName, user.Active.ToString() } 
}).ToArray() 
}; 
} 
return Json(jsonData, JsonRequestBehavior.AllowGet); 
} </string>
Posted
Comments
Philippe Mori 19-Jul-11 21:42pm    
Your second where clase should uses LastName instead of FirstName otherwise the code would be identical in both cases.

The simplest solution would be to have 2 returns statements.

If you prefer a cleaner approch, then create a private function for each case.

C#
public JsonResult LoadByName(string txtSearchVal, string SearchBy) 
{
    JsonResult result = null;

    if (SearchBy.ToUpper() == "FIRSTNAME") 
    { 
        result = LoadByFirstName(txtSearchVal);
    } 
    else if (SearchBy.ToUpper() == "LASTNAME") 
    { 
        result = LoadByLastName(txtSearchVal);
    }
    return result;
}

private JsonResult LoadByFirstName(string txtSearchVal)
{
    var jsonData = new
    {
        records = tskgrp.UserDetails.Count(),
        rows = (
            from user in tskgrp.UserDetails
            where (user.FirstName.StartsWith(txtSearchVal))
            select new
            {
                i = user.ID,
                cell = new string[] 
                { 
                     user.ID.ToString(), 
                     user.FirstName, 
                     user.LastName, 
                     user.Active.ToString() 
                }
            }).ToArray()
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

private JsonResult LoadByLastName(string txtSearchVal)
{
    var jsonData = new
    {
        records = tskgrp.UserDetails.Count(),
        rows = (
            from user in tskgrp.UserDetails
            where (user.LastName.Contains(txtSearchVal))
            select new  
            {
                i = user.ID,
                cell = new string[] 
                { 
                    user.ID.ToString(), 
                    user.FirstName, 
                    user.LastName, 
                    user.Active.ToString() 
                }
            }).ToArray()
    };

    return Json(jsonData, JsonRequestBehavior.AllowGet);
}
 
Share this answer
 
v2
Comments
Philippe Mori 19-Jul-11 21:41pm    
By the way, by declaring JsonResult result = null; as I have done, it is not necessary to split the function in 2 functions.
Also since your query only varies by the where clause, it is possible to simply use an OR operation to control the filtering.

C#
bool searchFirstName = false;
bool searchLastName = false;

switch (SearchBy.ToUpper())
{
    case "FIRSTNAME" :
        searchFirstName = true;
        break;

    case "LASTNAME" :
        searchLastName = true;
        break;

    case null:
    case "":
        // Unfiltered as is... Adjust if desired.
        break;

    default:
        // Could raise an exception here or return an
        // empty result set.
        break;
}

//...

var query =
    from user in tskgrp.UserDetails
    where !searchFirstName || user.FirstName.Contains(txtSearchVal)
    where !searchLastName || user.LastName.Contains(txtSearchVal)
    select new
    {
        // ...
    };

//...


Although it might be somewhat slower, it will greatly reduce code duplication when a trick like this is used for filtering.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900