【问题标题】:Calling HomeController action with Dependency injection from Razor Page Model从 Razor 页面模型使用依赖注入调用 HomeController 操作
【发布时间】:2019-02-23 06:05:45
【问题描述】:

我在HomeController 中有一个操作,Dependency InjecttionAsp.Net Core 2.1.0 Razor Page 中。

操作代码

    private readonly Test.Data.MyContext _Context;

    public HomeController(Test.Data.MyContext context)
    { _Context = context; }

    [HttpGet]
    public ActionResult TypeofAccounts(string at)
    {
        var result = _Context.TypeOfAccounts
            .Where(x => x.AccountType == at)
            .Select(x =>
                new
                {
                    label = x.AccountType,
                    id = x.AccountType
                }
            );

        return Json(result);
    }

我想在各种Razor PageModel 中使用这个结果。我怎样才能达到。这是示例 Razor 页面。

public class IndexModel : PageModel
{
    private readonly Test.Data.MyContext _Context;
    public IndexModel(Test.Data.MyContext context)
    { _Context = context; }

    public void OnGet()
    {
        // Here I want bind HomeController's action.
    }
}

我尝试使用var ta = new Test.Controllers.HomeController().TypeofAccounts("B001");,但没有成功。

【问题讨论】:

    标签: c# asp.net-core-2.1 razor-pages


    【解决方案1】:

    虽然我不熟悉在视图模型和控制器中都有数据上下文实例的做法,但您可以尝试这种方式。

    控制器:

    private readonly Test.Data.MyContext _Context;
    
    public HomeController(Test.Data.MyContext context)
    { _Context = context; }
    
    [HttpGet]
    public ActionResult TypeofAccounts(string at)
    {
        var result = GetTypeOfAccounts(_Context, at);
    
        return Json(result);
    }
    
    public static IQueryable<dynamic> GetTypeOfAccounts(Test.Data.MyContext context, string at)
    {
        var result = context.TypeOfAccounts
            .Where(x => x.AccountType == at)
            .Select(x =>
                new
                {
                    label = x.AccountType,
                    id = x.AccountType
                }
            );
    
        return result;
    }
    

    查看模型:

    public class IndexModel : PageModel
    {
        private readonly Test.Data.MyContext _Context;
        public IndexModel(Test.Data.MyContext context)
        { _Context = context; }
    
        public void OnGet()
        {
            // Here I want bind HomeController's action.
            var ta = Test.Controllers.HomeController.GetTypeOfAccounts(_Context, "B001");
        }
    }
    

    【讨论】:

    • 您的方法似乎有效,但不幸的是,我在 View Model 中收到错误“C# The type 'GetTypeOfAccounts' name does not exist in the type 'HomeController'”。
    • 用正确的命名空间替换Test.Controllers.。当我在示例中将public static 添加为public static 成员时,我看不出GetTypeOfAccountsHomeController 中不存在的原因。
    • 是的,添加正确,出于测试目的,我又添加了一个空的公共静态方法。两者都给了我同样的错误。
    • 这是对我有用的相同概念的屏幕截图。 postimg.cc/py5K7yGbpostimg.cc/LJxqhMC3 我无法重现您的错误。
    • 删除new。该方法是静态的,不是实例方法:var tb = GeneralModel.GetTypeOfAccounts(...)
    猜你喜欢
    • 1970-01-01
    • 2020-11-17
    • 2018-05-07
    • 2021-04-01
    • 2021-10-30
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多