如何在 ASP.NET MVC 中使用复选框创建选择列表?

发布于 2024-08-27 00:37:33 字数 545 浏览 4 评论 0原文

我有一个数据库表,记录允许用户访问哪些出版物。该表非常简单 - 它仅存储用户 ID/出版物 ID 对:

CREATE TABLE UserPublication (UserId INTEGER, PublicationID INTEGER)

给定用户的记录的存在和信息的存在。发布意味着用户有权访问;没有记录意味着无法访问。

我想向我的管理员用户展示一个简单的屏幕,允许他们配置用户可以访问哪些出版物。我想为每一种可能的出版物显示一个复选框,并检查用户当前可以访问的出版物。然后,管理员用户可以选中或取消选中任意数量的出版物并提交表单。

出版物类型有多种,我想将类似类型的出版物分组在一起 - 所以我确实需要控制出版物的呈现方式(我不想只有一个平面列表)。

我的视图模型显然需要有一个所有出版物的列表(因为我需要显示所有出版物,无论当前选择如何),并且我还需要用户当前有权访问的出版物的列表。 (我不确定使用单个列表是否会更好,其中每个项目都包含出版物 ID 和是/否字段?)。

但据我所知。我真的不知道如何将其绑定到某些复选框。我从哪里开始?

I have a database table that records what publications a user is allowed to access. The table is very simple - it simply stores user ID/publication ID pairs:

CREATE TABLE UserPublication (UserId INTEGER, PublicationID INTEGER)

The presence of a record for a given user & publication means that the user has access; absence of a record implies no access.

I want to present my admin users with a simple screen that allows them to configure which publications a user can access. I would like to show one checkbox for each of the possible publications, and check the ones that the user can currently access. The admin user can then check or un-check any number of publications and submit the form.

There are various publication types, and I want to group the similarly-typed publications together - so I do need control over how the publications are presented (I don't want to just have a flat list).

My view model obviously needs to have a list of all the publications (since I need to display them all regardless of the current selection), and I also need a list of the publications that the user currently has access to. (I'm not sure whether I'd be better off with a single list where each item includes the publication ID and a yes/no field?).

But that's as far as I've got. I've really no idea how to go about binding this to some checkboxes. Where do I start?

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

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

发布评论

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

评论(1

自控 2024-09-03 00:37:33

您的问题的 Linq to SQL 模型 看起来像这样:

替代文字

首先,我们的数据模型中需要一些辅助对象

namespace SelectProject.Models
{
    public class UserPublicationSelector
    {
        public int UserPublicationID { get; set; }
        public int UserID { get; set; }
        public int PublicationID { get; set; }
        public string PublicationName { get; set; }
        public bool IsSelected { get; set; }
    }

    public class UserPublicationSelectViewModel
    {
        public User User { get; set; }
        public IQueryable Selections { get; set; }
    }
}

现在让我们创建一个如下所示的存储库

public class Repository
{
    DataContext dc = new DataContext();

    public User GetUser(int userID)
    {
        return dc.Users.FirstOrDefault(u => u.UserID == userID);
    }

    public IQueryable GetUserPublications(int userID)
    {
        return from p in dc.Publications
               join up in dc.UserPublications on p.PublicationID equals up.PublicationID
               where up.UserID == userID
               orderby p.PublicationName
               select p;
    }
    public IQueryable GetUserPublicationSelectors(int userID)
    {
        return from p in dc.Publications
               join up in dc.UserPublications on p.PublicationID equals up.PublicationID into selected
               from s in selected.DefaultIfEmpty()
               orderby p.PublicationName
               select new UserPublicationSelector
               {
                   UserPublicationID = (int?)s.UserPublicationID ?? 0,
                   UserID = userID,
                   PublicationID = p.PublicationID,
                   PublicationName = p.PublicationName,
                   IsSelected = s.UserID != null
               };
    }

    public void UpdateUserPublications(UserPublicationSelector[] selections)
    {
        // Insert records for new selections...
        foreach (UserPublicationSelector selection in selections.Where(s => s.IsSelected == true))
        {
            // ...where records do not yet exist in database.
            if (selection.UserPublicationID == 0)
            {
                UserPublication up = new UserPublication
                {
                    UserID = selection.UserID,
                    PublicationID = selection.PublicationID,
                };
                dc.UserPublications.InsertOnSubmit(up);
            }
        }
        // Delete records for unselected items...
        foreach (UserPublicationSelector selection in selections.Where(s => s.IsSelected == false))
        {
            // ...where record exists in database.
            if (selection.UserPublicationID > 0)
            {
                UserPublication up = dc.UserPublications.FirstOrDefault(s => s.UserPublicationID == selection.UserPublicationID);
                if (up.UserID == selection.UserID && up.PublicationID == selection.PublicationID)
                    dc.UserPublications.DeleteOnSubmit(up);
            }
        }
        // Update the database
        dc.SubmitChanges();
    }
}

以及一个如下所示的控制器

public class PublicationController : Controller
{
    Repository repository = new Repository();

    public ActionResult Index(int id)
    {
        User user = repository.GetUser(id);
        var publications = repository.GetUserPublications(id);
        ViewData["UserName"] = user.UserName;
        ViewData["UserID"] = user.UserID;
        return View("Index", publications);
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Select(int id)
    {
        var viewModel = new UserPublicationSelectViewModel()
        {
            User = repository.GetUser(id),
            Selections = repository.GetUserPublicationSelectors(id)
        };
        return View("Select", viewModel);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Select(int userID, UserPublicationSelector[] selections)
    {
        repository.UpdateUserPublications(selections);
        return RedirectToAction("Index", new { id = userID });
    }
}

索引视图如下所示:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Publication>>" %>
<%@ Import Namespace="SelectProject.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    List of Selected Publications for User
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Publications for <%= ViewData["UserName"] %></h2>

        <table id="MyTable" style="width: 100%">
            <thead>
                <tr>
                    <th>
                        Publication Name
                    </th>
                </tr>
            </thead>

            <tbody>
                <%  int i = 0;
                    foreach (Publication item in Model)
                    { %>

                    <tr id="row<%= i.ToString() %>">
                        <td>
                            <%= Html.Encode(item.PublicationName)%>
                        </td>
                    </tr>

                    <% i++;
                    } %>
            </tbody>
        </table>
        <p>
            <%= Html.ActionLink("Edit Selections", "Select", new { id = ViewData["UserID"] })%>
        </p> 

</asp:Content>

选择视图如下所示:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UserPublicationSelectViewModel>" %>
<%@ Import Namespace="SelectProject.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Select Publications
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Select Publications for <%= Model.User.UserName %></h2>

    <% using (Html.BeginForm())
       { %>

        <table id="MyTable" style="width: 100%">
            <thead>
                <tr>
                    <th style="width: 50px; text-align:center">
                        <input type="checkbox" id="SelectAll" />
                    </th>
                    <th>
                        Publication Name
                    </th>
                </tr>
            </thead>

            <tbody>
                <%  int i = 0;
                    foreach (UserPublicationSelector item in Model.Selections)
                    { %>

                    <tr id="row<%= i.ToString() %>">
                        <td align="center" style="padding: 0 0 0 0">
                            <%= Html.CheckBox("selections[" + i.ToString() + "].IsSelected", item.IsSelected)%>
                            <%= Html.Hidden("selections[" + i.ToString() + "].UserPublicationID", item.UserPublicationID)%>
                            <%= Html.Hidden("selections[" + i.ToString() + "].UserID", Model.User.UserID)%>
                            <%= Html.Hidden("selections[" + i.ToString() + "].PublicationID", item.PublicationID)%>
                        </td>
                        <td>
                            <%= Html.Encode(item.PublicationName)%>
                        </td>
                    </tr>

                    <% i++;
                    } %>
            </tbody>
        </table>
        <p>
            <%= Html.Hidden("userID", Model.User.UserID) %>
            <input type="submit" value="save" />
        </p> 

    <% } // End Form %>

    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>

    <script type="text/javascript">
        // Select All Checkboxes
        $(document).ready(function() {
            $('#SelectAll').click(function() {
                var newValue = this.checked;
                $('input:checkbox').not('input:hidden').each(function() {
                    this.checked = newValue;
                });
            });
        });
    </script>

</asp:Content>

以下是一些屏幕截图。

替代文字

替代文本

左上角的复选框是全选/全选复选框。

The Linq to SQL model for your problem looks something like this:

alt text

First, we need some helper objects in our data model:

namespace SelectProject.Models
{
    public class UserPublicationSelector
    {
        public int UserPublicationID { get; set; }
        public int UserID { get; set; }
        public int PublicationID { get; set; }
        public string PublicationName { get; set; }
        public bool IsSelected { get; set; }
    }

    public class UserPublicationSelectViewModel
    {
        public User User { get; set; }
        public IQueryable Selections { get; set; }
    }
}

Now let's create a repository that looks like this:

public class Repository
{
    DataContext dc = new DataContext();

    public User GetUser(int userID)
    {
        return dc.Users.FirstOrDefault(u => u.UserID == userID);
    }

    public IQueryable GetUserPublications(int userID)
    {
        return from p in dc.Publications
               join up in dc.UserPublications on p.PublicationID equals up.PublicationID
               where up.UserID == userID
               orderby p.PublicationName
               select p;
    }
    public IQueryable GetUserPublicationSelectors(int userID)
    {
        return from p in dc.Publications
               join up in dc.UserPublications on p.PublicationID equals up.PublicationID into selected
               from s in selected.DefaultIfEmpty()
               orderby p.PublicationName
               select new UserPublicationSelector
               {
                   UserPublicationID = (int?)s.UserPublicationID ?? 0,
                   UserID = userID,
                   PublicationID = p.PublicationID,
                   PublicationName = p.PublicationName,
                   IsSelected = s.UserID != null
               };
    }

    public void UpdateUserPublications(UserPublicationSelector[] selections)
    {
        // Insert records for new selections...
        foreach (UserPublicationSelector selection in selections.Where(s => s.IsSelected == true))
        {
            // ...where records do not yet exist in database.
            if (selection.UserPublicationID == 0)
            {
                UserPublication up = new UserPublication
                {
                    UserID = selection.UserID,
                    PublicationID = selection.PublicationID,
                };
                dc.UserPublications.InsertOnSubmit(up);
            }
        }
        // Delete records for unselected items...
        foreach (UserPublicationSelector selection in selections.Where(s => s.IsSelected == false))
        {
            // ...where record exists in database.
            if (selection.UserPublicationID > 0)
            {
                UserPublication up = dc.UserPublications.FirstOrDefault(s => s.UserPublicationID == selection.UserPublicationID);
                if (up.UserID == selection.UserID && up.PublicationID == selection.PublicationID)
                    dc.UserPublications.DeleteOnSubmit(up);
            }
        }
        // Update the database
        dc.SubmitChanges();
    }
}

And a controller that looks like this:

public class PublicationController : Controller
{
    Repository repository = new Repository();

    public ActionResult Index(int id)
    {
        User user = repository.GetUser(id);
        var publications = repository.GetUserPublications(id);
        ViewData["UserName"] = user.UserName;
        ViewData["UserID"] = user.UserID;
        return View("Index", publications);
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Select(int id)
    {
        var viewModel = new UserPublicationSelectViewModel()
        {
            User = repository.GetUser(id),
            Selections = repository.GetUserPublicationSelectors(id)
        };
        return View("Select", viewModel);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Select(int userID, UserPublicationSelector[] selections)
    {
        repository.UpdateUserPublications(selections);
        return RedirectToAction("Index", new { id = userID });
    }
}

The Index view looks like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Publication>>" %>
<%@ Import Namespace="SelectProject.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    List of Selected Publications for User
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Publications for <%= ViewData["UserName"] %></h2>

        <table id="MyTable" style="width: 100%">
            <thead>
                <tr>
                    <th>
                        Publication Name
                    </th>
                </tr>
            </thead>

            <tbody>
                <%  int i = 0;
                    foreach (Publication item in Model)
                    { %>

                    <tr id="row<%= i.ToString() %>">
                        <td>
                            <%= Html.Encode(item.PublicationName)%>
                        </td>
                    </tr>

                    <% i++;
                    } %>
            </tbody>
        </table>
        <p>
            <%= Html.ActionLink("Edit Selections", "Select", new { id = ViewData["UserID"] })%>
        </p> 

</asp:Content>

And the Select view looks like this:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UserPublicationSelectViewModel>" %>
<%@ Import Namespace="SelectProject.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Select Publications
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Select Publications for <%= Model.User.UserName %></h2>

    <% using (Html.BeginForm())
       { %>

        <table id="MyTable" style="width: 100%">
            <thead>
                <tr>
                    <th style="width: 50px; text-align:center">
                        <input type="checkbox" id="SelectAll" />
                    </th>
                    <th>
                        Publication Name
                    </th>
                </tr>
            </thead>

            <tbody>
                <%  int i = 0;
                    foreach (UserPublicationSelector item in Model.Selections)
                    { %>

                    <tr id="row<%= i.ToString() %>">
                        <td align="center" style="padding: 0 0 0 0">
                            <%= Html.CheckBox("selections[" + i.ToString() + "].IsSelected", item.IsSelected)%>
                            <%= Html.Hidden("selections[" + i.ToString() + "].UserPublicationID", item.UserPublicationID)%>
                            <%= Html.Hidden("selections[" + i.ToString() + "].UserID", Model.User.UserID)%>
                            <%= Html.Hidden("selections[" + i.ToString() + "].PublicationID", item.PublicationID)%>
                        </td>
                        <td>
                            <%= Html.Encode(item.PublicationName)%>
                        </td>
                    </tr>

                    <% i++;
                    } %>
            </tbody>
        </table>
        <p>
            <%= Html.Hidden("userID", Model.User.UserID) %>
            <input type="submit" value="save" />
        </p> 

    <% } // End Form %>

    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>

    <script type="text/javascript">
        // Select All Checkboxes
        $(document).ready(function() {
            $('#SelectAll').click(function() {
                var newValue = this.checked;
                $('input:checkbox').not('input:hidden').each(function() {
                    this.checked = newValue;
                });
            });
        });
    </script>

</asp:Content>

Here are some screen shots.

alt text

alt text

The checkbox in the upper left hand corner is a Select All/Select None checkbox.

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