从CSHTML文件

时间:2016-11-08 20:26:33

标签: c# razor

我到处搜索,但无法弄清楚这里有什么问题。我有一个webb c#app。我在app文件夹中有一个c#方法,在那里我查询数据库,然后想要将该值传递回我的Cshtml页面,在那里我将基于该值进行makin deciaions。不管我做什么,当我调用方法然后尝试读取值时我得到错误"不能隐式转换类型' 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. 

1 个答案:

答案 0 :(得分:0)

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

为什么不尝试这样称呼:

var auth = class1.getdata();