通过复选框 WebFrom 和 Cookie 查看数据库列

发布于 2024-12-05 23:24:28 字数 3679 浏览 3 评论 0原文

我正在使用 telerik MVC 模板,并且有一个包含大量列的数据库,而 telerik 网格没有水平滚动条,因此我创建了复选框,供用户准确选择他们想要查看的列。它工作得很好,当我第一次进入该页面时,它会在顶部显示复选框,并在下面显示“应用”按钮和网格视图。由于 WebForm 尚未提交任何内容,因此网格视图显示所有列。在添加 cookie 之前,用户只需按一次“应用”即可只显示这些列。但是,如果用户随后尝试对其中一列进行排序或筛选,它将恢复显示所有列。因此,我创建了一个 cookie 来存储所选信息。不幸的是,这仅有助于选择第一个过滤器。如果使用第二个过滤器,它将再次显示所有列,而不仅仅是选定的列。此外,用户现在必须按两次“应用”才能使他们的选择正确显示在网格视图上。以下是我如何对所有内容进行编码的简要说明:

索引视图

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Index", "Order"))
   { %>
    <p>
        <%= Html.CheckBox("osurr", true, "Ad Number")%>Ad Number        //I set this and a few other columns to default to true
        <%= Html.CheckBox("csurr", false, "Customer Number")%>Customer Number
        <%= Html.CheckBox("rhosurr", false, "RHO Number")%>RHO Number
        <%= Html.CheckBox("lockid", false, "Lock ID")%>Lock ID
                                //And several more
    </p>
    <input type="submit" value="Apply" />
<% } %>
<%
    Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
                     {
                         columns.Template(o =>
                                              {
%>
                      <%=Html.ActionLink("Detail", "Edit", new { id = o.osurr })%>
                  <%
                                              }).Width(25);


                         if (Request.Cookies["DBCols"]["csurr"] != null)
                     {
                         if (Request.Cookies["DBCols"].Values["csurr"].Equals("True")) { columns.Bound(o => o.csurr).Title("Cust. No."); }
                     }
                     if (Request.Cookies["DBCols"]["rhosurr"] != null)
                     {
                         if (Request.Cookies["DBCols"].Values["rhosurr"].Equals("True")) { columns.Bound(o => o.rhosurr).Title("RHO No."); }
                     }
                     if (Request.Cookies["DBCols"]["lockid"] != null)
                     {
                         if (Request.Cookies["DBCols"].Values["lockid"].Equals("True")) { columns.Bound(o => o.lockid).Title("Lock ID"); }
                     }
                                                                                                    //And again, several more.
                     })
        .Groupable(grouping => grouping.Enabled(true))
        .Resizable(resizing => resizing.Columns(true))
        .Filterable(filter => filter.Enabled(true))
        .Sortable(sorting => sorting.Enabled(true))
        .Pageable(paging => paging.Enabled(true).PageSize(25))
        .Render();   
%>


</asp:Content>

控制器

public ActionResult Index(bool? csurr, bool? rhosurr, bool? lockid /* And Several More */)
{
ViewData["csurr"] = csurr ?? true;
ViewData["rhosurr"] = rhosurr ?? true;
ViewData["lockid"] = lockid ?? true;

if ((bool)ViewData["csurr"]) { DBCols.Values["csurr"] = (ViewData["csurr"].ToString());
}
else { DBCols.Values["csurr"] = "False"; }
if ((bool)ViewData["rhosurr"]) { DBCols.Values["rhosurr"] = (ViewData["rhosurr"].ToString()); }
else { DBCols.Values["rhosurr"] = "False"; }
if ((bool)ViewData["lockid"]) { DBCols.Values["lockid"] = (ViewData["lockid"].ToString()); }
else { DBCols.Values["lockid"] = "False"; }
//And Several more

var db = new MillieOrderDB();
var listView = from m in db.vw_cadords
orderby m.createstamp descending
select m;
return View(listView);
}

我现在只在索引 ActionResult 中工作,以便将所有内容保留在一个位置,同时弄清楚如何进行操作让这一切发挥作用。任何人都知道为什么我必须按应用两次,为什么我不能使用多个过滤器,以及如何避免这种情况?

I am using the telerik MVC template and have a database that has a huge number of columns and the telerik grid does not have a horizontal scroll bar, so I created checkboxes for the user to select exactly what columns they want to view. It works well enough in that when I first go to the page it shows the checkboxes at top with the Apply button and the grid view underneath. Because nothing has been submitted from the WebForm, the grid view shows all the columns. Before adding the cookies, the user only had to press apply once for only those columns to appear. However, if the user then tried to sort or filter one of these columns, it would revert back to showing all of the columns. So, I created a cookie to store the selected information. Unfortunately, this only helped with the selection of the first filter. If a second filter is used, it would, again, show all of the columns instead of just the ones selected. Furthermore, the user now has to press apply twice for their selections to show properly on the grid view. Here is a brief explanation of how I have everything coded:

Index View

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Index", "Order"))
   { %>
    <p>
        <%= Html.CheckBox("osurr", true, "Ad Number")%>Ad Number        //I set this and a few other columns to default to true
        <%= Html.CheckBox("csurr", false, "Customer Number")%>Customer Number
        <%= Html.CheckBox("rhosurr", false, "RHO Number")%>RHO Number
        <%= Html.CheckBox("lockid", false, "Lock ID")%>Lock ID
                                //And several more
    </p>
    <input type="submit" value="Apply" />
<% } %>
<%
    Html.Telerik().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
                     {
                         columns.Template(o =>
                                              {
%>
                      <%=Html.ActionLink("Detail", "Edit", new { id = o.osurr })%>
                  <%
                                              }).Width(25);


                         if (Request.Cookies["DBCols"]["csurr"] != null)
                     {
                         if (Request.Cookies["DBCols"].Values["csurr"].Equals("True")) { columns.Bound(o => o.csurr).Title("Cust. No."); }
                     }
                     if (Request.Cookies["DBCols"]["rhosurr"] != null)
                     {
                         if (Request.Cookies["DBCols"].Values["rhosurr"].Equals("True")) { columns.Bound(o => o.rhosurr).Title("RHO No."); }
                     }
                     if (Request.Cookies["DBCols"]["lockid"] != null)
                     {
                         if (Request.Cookies["DBCols"].Values["lockid"].Equals("True")) { columns.Bound(o => o.lockid).Title("Lock ID"); }
                     }
                                                                                                    //And again, several more.
                     })
        .Groupable(grouping => grouping.Enabled(true))
        .Resizable(resizing => resizing.Columns(true))
        .Filterable(filter => filter.Enabled(true))
        .Sortable(sorting => sorting.Enabled(true))
        .Pageable(paging => paging.Enabled(true).PageSize(25))
        .Render();   
%>


</asp:Content>

Controller

public ActionResult Index(bool? csurr, bool? rhosurr, bool? lockid /* And Several More */)
{
ViewData["csurr"] = csurr ?? true;
ViewData["rhosurr"] = rhosurr ?? true;
ViewData["lockid"] = lockid ?? true;

if ((bool)ViewData["csurr"]) { DBCols.Values["csurr"] = (ViewData["csurr"].ToString());
}
else { DBCols.Values["csurr"] = "False"; }
if ((bool)ViewData["rhosurr"]) { DBCols.Values["rhosurr"] = (ViewData["rhosurr"].ToString()); }
else { DBCols.Values["rhosurr"] = "False"; }
if ((bool)ViewData["lockid"]) { DBCols.Values["lockid"] = (ViewData["lockid"].ToString()); }
else { DBCols.Values["lockid"] = "False"; }
//And Several more

var db = new MillieOrderDB();
var listView = from m in db.vw_cadords
orderby m.createstamp descending
select m;
return View(listView);
}

I am working just in the Index ActionResult for now to keep things in one place while I figure out how to get this all to work. Anyone have any ideas why I am having to press apply twice, why I can not use more than one filter, and how to avoid this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2024-12-12 23:24:28

事实证明,我必须点击“应用”两次以及应用多个过滤器时导致问题的原因是因为我在 Index ActionResult 中拥有所有内容。一旦我将所有表单数据移至其自己的 ActionResult 中,然后执行 RedirecttoAction("Index"),一切都正常!

It turns out that the reason I had to hit apply twice and why when applying more than one filter caused issues was because I had everything in the Index ActionResult. Once I moved all the form data to its own ActionResult and then did RedirecttoAction("Index"), everything worked fine!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文