0

我想在视图的数据表中显示 ID 查找。为此,我执行以下操作:

  1. 创建将返回 ID 的函数:

    public int getClientID(string login)
    
        {
            var context = new MyEntity(new Uri("host/MyWCF.svc/"));
    
            var userID = from persons in context.PERSON
                         where persons.LOGIN == login
                         select persons.USER_ID;
    
            int uID = userID.First();
            var cli = from client in context.CLIENT
                      where client.USER_ID == uID
                      select client.CLIENT_ID;
            int cliID = cli.First();
            return cliID;
        } 
    
  2. 而且,在我看来:

    客户端:@{ ((HomeController)(this.ViewContext.Controller)).getClientID(User.Identity.Name); }

HomeController 是定义函数的控制器的名称。但是这样做,我有以下错误:

Can only specify query options (orderby, where, take, skip) after last navigation

在线int uID = userID.First();

那么,为什么我会出现这个错误(因为我在 te 视图中使用了好的命名空间),而且,这是一种很好的工作方式吗?谢谢 !

4

2 回答 2

1

您必须像这样在查询本身中记录第一条记录,

public int getClientID(string login)

    {
        var context = new MyEntity(new Uri("host/MyWCF.svc/"));

        var userID = (from persons in context.PERSON
                     where persons.LOGIN == login
                     select persons.USER_ID).FirstOrDefault(); //Take First ID here inside query 

        //So you can use userID directly in second query. 
        //But you have to check for null as well.

        var cli = (from client in context.CLIENT
                  where client.USER_ID == userID
                  select client.CLIENT_ID).FirstOrDefault();
        //int cliID = cli.First();
        return cli ;
    } 

要在 View 中轻松调用 Controller 方法,您也可以这样做,

在加载视图的控制器动作中(通常是它的索引动作)

添加此行以在 ViewBag 中创建新的动态函数。ViewBag.GetClientID = new Func(getClientId);

然后在你的控制器中添加方法

private int getClientId(string arg)
{
       //Your stuff
}

您可以像这样在 View 中调用此方法,

@ViewBag.GetClientID("Your Input string")

让我知道它是否有效?

于 2014-11-13T06:31:57.480 回答
1
@Html.Action("Home","getClientID", new {login=User.Identity.Name})
于 2013-06-26T19:21:26.323 回答