【问题标题】:Implementing a model with dynamic columns使用动态列实现模型
【发布时间】:2018-03-15 03:05:40
【问题描述】:

我正在尝试使用动态列实现一种模型。

我的模特:

public partial class AccountViewModel : DbContext
{
    public ACCOUNT Accountdt { get; set; }
}

我的控制器代码返回 index.cshtml:

// To display grid in Kendo UI
public ActionResult Accounts_Read([DataSourceRequest]DataSourceRequest request)
{
    Json(GetAccounts().ToDataSourceResult(request,ModelState));
}

//To extract data from linq
private IEnumerable<Object> GetAccounts()
{
    var database = new Entities();
    var load = database.ACCOUNTs.AsQueryable();
    AccountViewModel avm = new AccountViewModel();
    return load.Select(account => avm.Accountdt);
}

我无法在结果数据网格中看到数据集。早期在模型中声明列时它工作正常。

这是我运行良好的代码:

public partial class AccountViewModel : DbContext
{
    public string Id { get; set; }
}

// To display grid in Kendo UI
public ActionResult Accounts_Read([DataSourceRequest]DataSourceRequest request)
{
    Json(GetAccounts().ToDataSourceResult(request, ModelState));
}

private IEnumerable<Object> GetAccounts()
{
    var database = new Entities();
    var load = database.ACCOUNTs.AsQueryable();

    return load.Select(account => AccountViewModel {
        id = account.id;
    });
}

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

    更改以下代码,

    return load.Select(account => avm.Accountdt);
    

    如下,

    return load.Select(account => new AccountViewModel {
        Accountdt = account
    });
    

    【讨论】:

    • 感谢您的回答。我已经尝试过这段代码,但是......它不起作用。发生运行时错误。在 Json(GetAccounts(SearchPar1).ToDataSourceResult(request,ModelState)); ----------------------- 错误消息是不支持发生异常
    • Json(GetAccounts(SearchPar1).ToDataSourceResult(request,ModelState)); 运行时出现不支持的异常
    • 这个 SearchPar1 代表什么??? – Abhilash Ravindran C K 6 分钟前 没关系..(使用 linq 搜索查询)。它只是不使用变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    相关资源
    最近更新 更多