【问题标题】:Dynamic column selection as per user input in asp.net mvc根据 asp.net mvc 中的用户输入动态选择列
【发布时间】:2020-05-24 23:03:04
【问题描述】:

我正在向用户显示一个屏幕,他可以在其中检查他想从数据库中获取哪些字段。基于选中的复选框,我想使用 LINQ 或 lambda 表达式编写查询并在 ASP.NET MVC 中获取数据。由于列选择是动态的,如何做到这一点?例如,如果只检查了 Name 和 Email 列,则仅从数据库中获取这 2 列的数据。

控制器

public ActionResult Report()
{
    return View();
}

[HttpPost]
public ActionResult Report(Employee emp)
{
    List<Employee> employees = new List<Employee>();

    employees = db.Employees.Select(x => new Employee()
                {
                    // select Only those columns which are checked by the user
                }).ToList();
    return View();
}

查看

@model IEnumerable<DynamicCheck.Models.Employee>
@{
    ViewBag.Title = "EmployeeReport";
}

<h2>EmployeeReport</h2>

<form action="~/Employees/Report" method="post">
    <div>
        <div class="col-lg-3">
            <input type="checkbox" name="Name" />
            <label>Employee Name</label>
        </div>
        <div class="col-lg-3">
            <input type="checkbox" name="Email" />
            <label>Employee Email</label>
        </div>
        <div class="col-lg-3">
            <input type="checkbox" name="Address" />
            <label>Employee Address</label>
        </div>
        <div class="col-lg-3">
            <input type="checkbox" name="Phone" />
            <label>Employee Name</label>
        </div>

        <input type="submit" value="Submit"/>
    </div>
</form>

【问题讨论】:

    标签: c# sql asp.net asp.net-mvc entity-framework


    【解决方案1】:

    您应该使用 javascript 并检查发送给查看的列。 并动态创建 html 元素...

    【讨论】:

      猜你喜欢
      • 2010-09-22
      • 2023-04-09
      • 1970-01-01
      • 2021-02-07
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多