asp.net mvc:使用 formcollection 创建列表框

发布于 2024-11-30 06:10:00 字数 2335 浏览 4 评论 0原文

在我看来,我遇到了当前错误:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  Inherits="System.Web.Mvc.ViewPage<ProjectenII.Models.Domain.StudentModel>"%>

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

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

  <%using (Html.BeginForm()) { %>
    <%=Html.ListBoxFor(model => model.NormalSelected, new MultiSelectList(Model.NormalStudentsList, "StudentNummer", "Naam", Model.NormalSelected), new { size = "6" }); %>

    <input type="submit" name="add" 
                          id="add" value=">>" /><br />
    <input type="submit" name="remove" 
                          id="remove" value="<<" />
    <%=Html.ListBoxFor(model => model.NoClassSelected, new MultiSelectList(Model.StudentsNoClassList, "StudentNummer", "Naam", Model.NoClassSelected)); %>
  <% } %>

  <%=Html.HiddenFor(model => model.Save) %>
  <input type="submit" name="apply" id="apply" value="Save!" />
</asp:Content>

它在 listboxfor() 方法中给了我一个错误......说“)预期”。

但我关闭了所有的开始标签......虽然很奇怪! 我想用它做什么:我想将项目从一个列表框移动到另一个列表框,然后更新数据库。所以我想使用 formCollection 来做到这一点,除非还有其他方法吗?

学生有一个名为“classID”的字段,当我更新数据库时,该值需要从当前值更改为“0”。我认为最好的方法是使用 formCollections?不是吗? 这是我的 StudentModel

   public class StudentModel
    {
        public IEnumerable<Student> NormalStudentsList { get; set; }
        public IEnumerable<Student> StudentsNoClassList { get; set; }
        public string[] NormalSelected { get; set; }
        public string[] NoClassSelected { get; set; }
        public string Save { get; set; }
    }

控制器:

 public ActionResult IndexStudents(Docent docent, int id, int klasgroepid)
        {
            var studentModel = new StudentModel
            {
               NormalStudentsList = docent.GeefStudenten(id, klasgroepid),
               StudentsNoClassList = docent.GeefStudenten(id, klasgroepid)
            };

            return View(studentModel);
        }

我有两个问题:如何修复错误?我怎样才能更新数据库?

我建议使用“UpdateModel()”...?

提前致谢!!

I'm experiencing current error in my view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  Inherits="System.Web.Mvc.ViewPage<ProjectenII.Models.Domain.StudentModel>"%>

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

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

  <%using (Html.BeginForm()) { %>
    <%=Html.ListBoxFor(model => model.NormalSelected, new MultiSelectList(Model.NormalStudentsList, "StudentNummer", "Naam", Model.NormalSelected), new { size = "6" }); %>

    <input type="submit" name="add" 
                          id="add" value=">>" /><br />
    <input type="submit" name="remove" 
                          id="remove" value="<<" />
    <%=Html.ListBoxFor(model => model.NoClassSelected, new MultiSelectList(Model.StudentsNoClassList, "StudentNummer", "Naam", Model.NoClassSelected)); %>
  <% } %>

  <%=Html.HiddenFor(model => model.Save) %>
  <input type="submit" name="apply" id="apply" value="Save!" />
</asp:Content>

It gives me an error at the listboxfor() method... saying ") expected".

But I close all the opening tags... very strange though!
What I want to use it for: I want to move items from one listbox to the other and then update the database. So I'd like to do it using formCollection, unless there is another way?

Students have a field named "classID", when I update the database, that value needs to change from the current value to "0". I think the best way is using formCollections? Isn't it?
This is my StudentModel

   public class StudentModel
    {
        public IEnumerable<Student> NormalStudentsList { get; set; }
        public IEnumerable<Student> StudentsNoClassList { get; set; }
        public string[] NormalSelected { get; set; }
        public string[] NoClassSelected { get; set; }
        public string Save { get; set; }
    }

Controller:

 public ActionResult IndexStudents(Docent docent, int id, int klasgroepid)
        {
            var studentModel = new StudentModel
            {
               NormalStudentsList = docent.GeefStudenten(id, klasgroepid),
               StudentsNoClassList = docent.GeefStudenten(id, klasgroepid)
            };

            return View(studentModel);
        }

I have two questions: how can I fix the error? AND how can I update the database?

I suggest using "UpdateModel()" ... ?

Thanks in advance!!

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

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

发布评论

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

评论(1

莫多说 2024-12-07 06:10:00

不确定您的第二个问题是什么,因为您没有包含用于将模型保存到数据库的代码。

“)”预期错误是因为 ListBoxFor 方法调用末尾有一个分号。

它应该看起来像这样:

<%=Html.ListBoxFor(model => model.NormalSelected, new MultiSelectList(Model.NormalStudentsList, "StudentNummer", "Naam", Model.NormalSelected), new { size = "6" }) %>

当您使用 <%= 时,您不需要分号。

Not sure what your second question is because you didn't include the code you're using to persist your model to the database.

The ")" expected error is because you have a semicolon at the end of your ListBoxFor method call.

It should look like this:

<%=Html.ListBoxFor(model => model.NormalSelected, new MultiSelectList(Model.NormalStudentsList, "StudentNummer", "Naam", Model.NormalSelected), new { size = "6" }) %>

When you use <%= you don't need the semicolon.

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