MVC3 复选框列表

发布于 2024-12-27 14:20:12 字数 578 浏览 4 评论 0原文

可能的重复:
MVC3.0中的CheckboxList

嗨,我有两门课书和作者,我想要的是当你添加一本书会出现作者列表,您可以选择其中的一个或多个。我认为最好的方法是使用清单,但没有找到使用 mvc3 执行此操作的方法。但读过一些例子但不明白,我只是从 mvc 开始,所以如果有人能告诉我如何制作这些,我将不胜感激

public class Book
 {
     public int IdBook {get; set;}
     public string Title {get; set;}
     public List<author> Authors  {get; set;}
 }


public class Author
{
    public int IdAuthor{get; set;}
    public string Name {get; set;}
}

Possible Duplicate:
CheckboxList in MVC3.0

Hi I have two classes book and author and I want is that when you add a book appears a list of authors and you can select one or more of one. the best way to do this I think is a checklist but not found a way to do this with mvc3. but have read some examples and not understood, I just starting with mvc so if someone can tell me how could I make these I would greatly appreciate

public class Book
 {
     public int IdBook {get; set;}
     public string Title {get; set;}
     public List<author> Authors  {get; set;}
 }


public class Author
{
    public int IdAuthor{get; set;}
    public string Name {get; set;}
}

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

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

发布评论

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

评论(2

您的好友蓝忘机已上羡 2025-01-03 14:20:12

您可以向您的作者添加一个 Selected 布尔属性,以便了解他是否被选为给定的书籍:

public class Author
{
    public int IdAuthor { get; set; }
    public bool Selected { get; set; }
    public string Name { get; set; }
}

然后:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var book = new Book
        {
            IdBook = 1,
            Title = "foo bar",
            Authors = new[]
            {
                new Author { IdAuthor = 1, Name = "author 1" },
                new Author { IdAuthor = 2, Name = "author 2" },
                new Author { IdAuthor = 3, Name = "author 3" },
            }.ToList()
        };
        return View(book);
    }

    [HttpPost]
    public ActionResult Index(Book book)
    {
        return View(book);
    }
}

并在视图中:

@model Book

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Title)
        @Html.EditorFor(x => x.Title)
    </div>

    for (int i = 0; i < Model.Authors.Count; i++)
    {
        @Html.CheckBoxFor(x => x.Authors[i].Selected)
        @Html.LabelFor(x => x.Authors[i].Selected, Model.Authors[i].Name)    
        @Html.HiddenFor(x => x.Authors[i].Name)
    }

    <p>
        <button type="submit">OK</button>
    </p>
}

You could add a Selected boolean property to your Author in order to know whether he was selected for the given book or not:

public class Author
{
    public int IdAuthor { get; set; }
    public bool Selected { get; set; }
    public string Name { get; set; }
}

and then:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var book = new Book
        {
            IdBook = 1,
            Title = "foo bar",
            Authors = new[]
            {
                new Author { IdAuthor = 1, Name = "author 1" },
                new Author { IdAuthor = 2, Name = "author 2" },
                new Author { IdAuthor = 3, Name = "author 3" },
            }.ToList()
        };
        return View(book);
    }

    [HttpPost]
    public ActionResult Index(Book book)
    {
        return View(book);
    }
}

and in the view:

@model Book

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.Title)
        @Html.EditorFor(x => x.Title)
    </div>

    for (int i = 0; i < Model.Authors.Count; i++)
    {
        @Html.CheckBoxFor(x => x.Authors[i].Selected)
        @Html.LabelFor(x => x.Authors[i].Selected, Model.Authors[i].Name)    
        @Html.HiddenFor(x => x.Authors[i].Name)
    }

    <p>
        <button type="submit">OK</button>
    </p>
}
a√萤火虫的光℡ 2025-01-03 14:20:12

您需要向您的作者模型视图类添加一个 bool Selected 字段(或为其创建一个模型视图类,以便您可以存储此类元数据),然后就可以了像往常一样正常绑定:

@foreach(var book in Model.Books)
{
  <table>
    <!-- fill in the book details and create the checkbox list template -->

    @foreach(var author in book.Authors)
    {
      <tr><td>
        @Html.CheckBoxFor(x=>x.Selected);   <!-- the checkbox -->
      </td><td>
        @Html.DisplayFor(x=>x);            <!-- the description -->
                                           <!-- could also go with a nice partial view here -->
      </td></tr>
    }
  </table>
}

You'll need to add a bool Selected field to your author model-view class (or create a model-view class for it so you can store this kind of meta-data), and then it's just normal binding like usual:

@foreach(var book in Model.Books)
{
  <table>
    <!-- fill in the book details and create the checkbox list template -->

    @foreach(var author in book.Authors)
    {
      <tr><td>
        @Html.CheckBoxFor(x=>x.Selected);   <!-- the checkbox -->
      </td><td>
        @Html.DisplayFor(x=>x);            <!-- the description -->
                                           <!-- could also go with a nice partial view here -->
      </td></tr>
    }
  </table>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文