从CSHTML文件中调用另一个类中的方法

扬夫

我到处搜寻过,但无法找出问题所在。我有一个webb c#应用程序。我在app文件夹中有一个ac#方法,我在其中查询数据库,然后希望将该值传递回我的Cshtml页面,在该页面中,我将根据该值进行决策。当我调用该方法然后尝试读取该值时,没有做任何事情,我收到错误消息“无法将类型'class1'隐式转换为字符串。

这是我的c#class方法,在调用下方

方法:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class class1

    {
    public static string getdata()
    {

       string  cnnString = ConfigurationManager.ConnectionStrings["GDC_WellsFargoConnectionString"].ConnectionString;
        SqlDataReader reader;
        string returnValue;



        /// var cmd = "insert into Email Insert values(@name,@email";
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        using (SqlConnection cnn = new SqlConnection(cnnString))
        {

            using (SqlCommand cmd = new SqlCommand("Select isnull([Authorization],'none') as authoriztion from PNSWebAuthorization where username =  '" + userName + "'", cnn))
            {
                ////cmd.Parameters.AddWithValue("@name", "Newname,First");
                ////   cmd.Parameters.AddWithValue("@email", "[email protected]");

                ///string authcode;
                cnn.Open();
               try { 
      reader = cmd.ExecuteReader();

                    if (reader.Read())
                    {
                        returnValue = reader["Authorization"].ToString();
                        reader.Close();


                        return returnValue;
                    }
                    else
                    {
                        reader.Close();
                        return "";
                    }
                }
              catch (Exception err)
                {
                    throw new ApplicationException(err.Message);
                }
                finally
                {

                    cnn.Close();
                    cnn.Dispose();
                }


            }
        }
    }



CSHTML Calling

@model GDC.Finance.WebClient.Areas.Treasury.ViewModels.CashReceiptSessionListViewModel
@using PagedList.Mvc;


@{
    ViewBag.Title = "Cash Receipt Sessions test";
}

<h2>Sessions</h2>

@section scripts{
    <script src="~/Scripts/popup.js"></script>

}
@{

    /// string auth = "none";
    var auth = new class1();
    class1.getdata();

    string rights = auth;



}

Auth throws the error. 
代约·奥科索拉

看起来“公共静态字符串getdata()”是一个静态方法。

为什么不尝试以这种方式调用它:

var auth = class1.getdata();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在另一个文件的另一个类中调用 php 方法?

如何从另一个.java文件中的另一个类调用方法?

Repaint()方法在另一个类中调用

从UIButton中的另一个类调用方法

在PHP的另一个类中调用方法

未定义的方法-从另一个文件中调用一个文件中的类

如何从另一个 python 文件中的一个 python 文件中的类调用方法

如何从另一个类中迅速调用另一个方法?

在Ruby的另一个类中的一个类中调用方法

如何从Python中的另一个文件调用类方法?

在另一个文件中调用类方法时,是否必须提供self的值?

Python-如何从另一个新修改的python文件中调用类方法

覆盖另一个文件中的类的方法

从Python 2.7中的另一个类方法调用一个类方法

从另一个类中的一个类调用方法

如何在另一个类中调用一个类的main()方法?

从另一个类中的一个类调用方法

Javascript:Phaser在另一个方法类中调用一个方法

在同一个类的另一个方法中调用一个类的方法的对象

如何从Android中的另一个片段类调用一个片段的方法

Swift:创建一个按钮来调用另一个类中的方法

在python的同一类中的另一个方法中调用一个类的方法

另一个类的模板成员函数中的模板类中的调用方法

稍后在 C++ 中的另一个类方法中调用类构造函数

如何在python中的另一个文件中调用类的方法[单元测试]

如何调用另一个.java文件中存在的类(不是嵌套类)?

在另一个类的方法中调用类实例的正确方法是什么?

调用另一个文件中的类和函数以编辑该类中的变量

Python - 在另一个文件中的类中调用定义