如何在JsonResult上返回1个值

时间:2014-01-06 09:08:17

标签: javascript html json asp.net-mvc-3 linq

我遇到问题,我需要使用JsonResult,但会返回单个值..

我可以问这是否可能,我应该如何在我的代码集中使用它

我想返回地址,但我不知道是否可能。 。

public JsonResult LoadAddress()
        {

            Guid userId = (Guid)Membership.GetUser().ProviderUserKey;
            if (User.IsInRole("Administrator"))
            {
                var address = (from a in db.Clients
                               join b in db.Client_Location
                               on a.ID equals  b.ClientID
                               where a.userId == userId
                                     && a.LogicalDelete == false
                               select a).SingleOrDefault();
            }
            else if (User.IsInRole("Worker"))
            {
                var address = (from a in db.Workers
                               join b in db.Worker_Address
                               on a.ID equals  b.WorkerID
                               where a.userId == userId &&
                               a.LogicalDelete == false
                               select a).SingleOrDefault();

            }
            else if (User.IsInRole("Employer"))
            {
                var address = (from a in db.Client_Customer
                               join b in db.Client_Customer_Location
                               on a.ID equals b.Client_CustomerID
                               where a.userId == userId
                                     && a.LogicalDelete == false
                               select a).SingleOrDefault();
            }
            return Json(address, JsonRequestBehavior.AllowGet);
        }

提前谢谢

3 个答案:

答案 0 :(得分:0)

public JsonResult LoadAddress()
        {
            Address address = null;
            Guid userId = (Guid)Membership.GetUser().ProviderUserKey;

            if (User.IsInRole("Administrator"))
            {
                address = (from a in db.Clients
                               join b in db.Client_Location
                               on a.ID equals  b.ClientID
                               where a.userId == userId
                                     && a.LogicalDelete == false
                               select a).SingleOrDefault();
            }
            else if (User.IsInRole("Worker"))
            {
                address = (from a in db.Workers
                               join b in db.Worker_Address
                               on a.ID equals  b.WorkerID
                               where a.userId == userId &&
                               a.LogicalDelete == false
                               select a).SingleOrDefault();

            }
            else if (User.IsInRole("Employer"))
            {
                address = (from a in db.Client_Customer
                               join b in db.Client_Customer_Location
                               on a.ID equals b.Client_CustomerID
                               where a.userId == userId
                                     && a.LogicalDelete == false
                               select a).SingleOrDefault();
            }
            return Json(address, JsonRequestBehavior.AllowGet);
        }

答案 1 :(得分:0)

假设您拥有所有不同地址类型的公共接口(例如ClientsWorkersClient_Customer),您可以这样做:

public interface IAddress{
    string Name{get;set;}
    string Street {get; set;}
    // etc
}

然后,如果所有相关地址类型都实现了接口,那么您只需执行以下操作:

IAddress address = null;
if (User.IsInRole("Administrator"))
{
    address = (IAddress)(from a in db.Clients... etc)
}
else if (User.IsInRole("Worker"))
{
    address = (IAddress)(from a in db.Workers... etc)

}
else if (User.IsInRole("Employer"))
{
    address = (IAddress)(from a in db.Client_Customer... etc)
}
else
{
    address = /// some other default that you want
}
return Json(address, JsonRequestBehavior.AllowGet);

[edit] - 这种情况的典型实现看起来也是如此,您需要按照上面的编辑(即(IAddress)(etc))展示您的结果:

public class Client : IAddress {
    public string Name{get;set;}
    public string Street {get; set;}
    // etc
    /* now all your other unique properties*/
    public string ClientName{get;set;}    
}

public class Client_Customer : IAddress {
    public string Name{get;set;}
    public string Street {get; set;}
    // etc
    /* now all your other unique properties*/
    public string AnotherName{get;set;}    
}

public class Workers: IAddress {
    public string Name{get;set;}
    public string Street {get; set;}
    // etc
    /* now all your other unique properties*/
    public string WorkerName{get;set;}    
}

答案 2 :(得分:0)

您可以使用ExpandoObject

dynamic expando = new ExpandoObject();
if (User.IsInRole("Administrator"))
{
    expando.address = ...
}
else if (User.IsInRole("Worker"))
{
    expando.address = ...

}
else if (User.IsInRole("Employer"))
{
    expando.address = ...
}
return Json(expando.address, JsonRequestBehavior.AllowGet);