Asp.net mvc 4 MVC4:在视图中调用控制器函数结果

Asp.net mvc 4 MVC4:在视图中调用控制器函数结果,asp.net-mvc-4,razor,visual-studio-2012,Asp.net Mvc 4,Razor,Visual Studio 2012,我想在视图的datatable中显示一个ID find。为此,我采取以下措施: 创建将返回ID的函数: public int getClientID(string login) { var context = new MyEntity(new Uri("host/MyWCF.svc/")); var userID = from persons in context.PERSON where persons.LO

我想在视图的datatable中显示一个ID find。为此,我采取以下措施:

  • 创建将返回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;
        } 
    
  • 在我看来:

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

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

    Can only specify query options (orderby, where, take, skip) after last navigation
    
    在第行
    intuid=userID.First()

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

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

    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 ;
        } 
    
    @ViewBag.GetClientID("Your Input string")
    

    要轻松调用控制器方法inside View,您也可以这样做

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

    添加此行可在ViewBag内创建新的动态功能。 ViewBag.GetClientID=新函数(GetClientID)

    然后在控制器中添加方法

    private int getClientId(string arg)
    {
           //Your stuff
    }
    
    您可以像这样在视图内部调用此方法

    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 ;
        } 
    
    @ViewBag.GetClientID("Your Input string")
    

    让我知道它是否有效?

    您应该首先尝试了解MVC模式……我知道,融合视图和控制器根本不是一个好主意!如何快速进行?提示:
    getClientId
    方法属于模型。控制器应该调用它并将ID传递给视图。