使DropDownLists彼此依赖

时间:2021-12-10 14:08:13

In my MVC4 application I have two models being User and Schedule that look like this:

在我的MVC4应用程序中,我有两个模型,分别是User和Schedule,如下所示:

public class User {

    public int UserId { get; set; }
    public string Name { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public bool isAvailable { get; set; }
}

public class Schedule 
{
    public int ScheduleId{ get; set; }
    public DateTime Date { get; set; }
    public int UserId { get; set; }
    public virtual User User { get; set; }
}  

Now I want to make a page that will display a table with the days of this month and next to the day a DropDownLists (per day) with all the users with isAvailable to true in the list. This page will be used so that an admin can make a schedule (I would also be open for improvements on how to implement this) and select one user per weekday. I want to make it so that once a user is selected in one DropDownList that this user disappears from the other DropDownLists.

现在我想创建一个页面,显示一个表格,其中包含本月的日期和下一天的DropDownLists(每天)以及列表中isAvailable为true的所有用户。将使用此页面以便管理员可以制定计划(我也将对如何实施此计划进行改进)并在每个工作日选择一个用户。我希望这样做,以便在一个DropDownList中选择一个用户后,该用户将从其他DropDownLists中消失。

The Controller and View looks as follows now, controller:

Controller和View现在看起来如下,控制器:

public ActionResult Schedule()
    {
        ViewBag.UserId = new SelectList(db.Users, "UserId", "FullName");
        return View();
    }

View:

视图:

@model IEnumerable<seashell_brawl_corvee.Models.Schedule>

@{
    ViewBag.Title = "Schedule";

}

<fieldset>
<legend>@ViewBag.Title</legend>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <table class="table">
        <thead>
            <tr>
                <th>Date
                </th>
                <th>Select person
                </th>
            </tr>
        </thead>

        @{ DateTime dt = DateTime.Now; }

        @for (double i = 0.0; i < 60.0; i++)
        {
            dt = dt.AddDays(1);
            if (dt.Date.DayOfWeek != DayOfWeek.Saturday &&
                dt.Date.DayOfWeek != DayOfWeek.Sunday)
            {
            <tbody>
                <tr>
                    <td>
                        @dt.Date.ToLongDateString()
                    </td>

                        <td>
                          @Html.DropDownList("UserId", String.Empty)
                        </td>               

                </tr>
            </tbody>
            }
            else
            {
            <tbody>
                <tr></tr>
            </tbody>    
            }
        }
    </table>

    @TODO: Submit button and controller POST action that reads the dates and names 
           from the dropdownlists, puts them into arrays and then into the database.
}

</fieldset>

I know I need JavaScript/JSON to make the DropDownLists dependent on each other and have names no longer displayed once they are selected, but I do not know how exactly I would go to accomplish this. I would be very grateful if someone could help me further!

我知道我需要JavaScript / JSON来使DropDownLists彼此依赖,并且一旦选中它们就不再显示名称,但我不知道我将如何完成此任务。如果有人能帮助我,我将非常感激!

1 个解决方案

#1


2  

I have an approach using JQuery, Check this example: http://jsfiddle.net/vt3Ma/4/

我有一个使用JQuery的方法,请查看此示例:http://jsfiddle.net/vt3Ma/4/

using focus() event will restore the current option value and text in 2 hidden fields

使用focus()事件将恢复当前选项值和2个隐藏字段中的文本

$('.ddl').focus(function(){
$("#oldValue").val($(this).val());
$("#oldText").val($("option:selected",$(this)).text());

});

});

then on change() event:-

然后在change()事件: -

1- will add the old changed option of the dropdown list to the other dropdown lists as the previous selected option became available now to choose.

1-将下拉列表的旧更改选项添加到其他下拉列表,因为之前选择的选项现在可供选择。

$(this).prepend("<option value='"+$("#oldValue").val()+"'>"+$("#oldText").val()+"</option>");

2- will remove the new selected option from all other dropdown lists

2-将从所有其他下拉列表中删除新选择的选项

$("option[value='" + current.val() + "']",$(this)).remove();

#1


2  

I have an approach using JQuery, Check this example: http://jsfiddle.net/vt3Ma/4/

我有一个使用JQuery的方法,请查看此示例:http://jsfiddle.net/vt3Ma/4/

using focus() event will restore the current option value and text in 2 hidden fields

使用focus()事件将恢复当前选项值和2个隐藏字段中的文本

$('.ddl').focus(function(){
$("#oldValue").val($(this).val());
$("#oldText").val($("option:selected",$(this)).text());

});

});

then on change() event:-

然后在change()事件: -

1- will add the old changed option of the dropdown list to the other dropdown lists as the previous selected option became available now to choose.

1-将下拉列表的旧更改选项添加到其他下拉列表,因为之前选择的选项现在可供选择。

$(this).prepend("<option value='"+$("#oldValue").val()+"'>"+$("#oldText").val()+"</option>");

2- will remove the new selected option from all other dropdown lists

2-将从所有其他下拉列表中删除新选择的选项

$("option[value='" + current.val() + "']",$(this)).remove();