一个cshtml如何在另一个cshtml中调用一个函数?

时间:2012-03-22 21:25:05

标签: function razor webmatrix

在webmatrix中,我在App_Data中创建了一个包含大量公共静态函数的DAL.cshtml。

我想从另一个文件夹中的另一个.cshtml页面调用它们。

现在我明白了 编译器错误消息:CS0103:当前上下文中不存在名称DAL

(请注意,我是关于将数据访问权移至单个位置的博客的初学者)

在我的DAL.cshtml中,一个例子是:

public static void AddProfile (dynamic Profile) 
{
var sql = "INSERT INTO profile (ProfileDescription, ProfileType) " +
            "VALUES (@0, @1)";
            PinwheelDB.Execute(sql, Profile.ProfileDescription, Profile.ProfileType);
            var Profile.ProfileID = PinwheelDB.GetLastInsertId();
}

在我的'调用'.cshtml中,我有:

        dynamic Profile = new ExpandoObject();
            Profile.ProfileDescription = Request.Form["txtChildFirstName"];
            Profile.ProfileType = 1;
        functions.DAL.AddProfile(Profile);
        var vProfileID = Profile.ProfileID;

2 个答案:

答案 0 :(得分:2)

一旦我将DAL.cshtml移动到一个名为App_Code而不是App_Data(感谢Darin)的文件夹中,它就运行了。

请注意,我最终得到的代码如下(如果它有助于其他初学者)........

DAL.cshtml现在位于名为App_Code的文件夹中我有:

@functions 
{
public static void AddProfile (dynamic Profile) 
{
var sql = "......"                
PinwheelDB.Execute(sql, .......);
}

调用.cshtml 仍然是我第一次引用

如果有人知道为什么这是我想要理解的。

答案 1 :(得分:0)

您可以将常用方法放在App_Code文件夹的类中。例如,您可以拥有~/App_Code/DAL.cs

public class DAL
{
    public static void AddProfile(dynamic profile)
    {
        ...
    }
}

并在你的Razor调用页面中:

@{
    var profile = ...
    DAL.AddProfile(profile);
}