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

本文关键字:方法 另一个 CSHTML 文件 调用 | 更新日期: 2023-09-27 18:16:56

我到处都找过了,但不知道这里出了什么问题。我有一个韦伯c#应用程序。我有一个c#方法在应用程序文件夹中,我查询数据库,然后要传递该值回我的Cshtml页面,在那里我将根据值做出决定。不管我做什么,当我调用该方法然后尝试读取值时,我得到错误"不能隐式地将类型'class1'转换为字符串。

这是我的c#类方法和下面的调用

方法:

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", "Newemail@gibsondunn.com");
                ///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. 

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

看起来"public static string getdata()"是一个静态方法

为什么不试试这样调用呢:

var auth = class1.getdata();