编辑多个记录 - asp.net MVC

时间:2017-08-31 14:30:48

标签: c# asp.net-mvc asp.net-mvc-4 razor

用这个把我的头撞了几天,似乎无法弄明白或找到一篇回答我问题的文章。

我正在使用rota系统进行工作,这几乎已经完成了,但是我在管理区域停留在这一部分。 它允许管理人员为给定的团队编辑给定日期范围的多个rota条目。 它最终将使用视图模型传递dateFrom,dateTo和Team(teamID) - 我现在只是这样做,试图让它工作。

rotas存储在Rotas表中(见下文)。 Rotas table

MorningShift和EveningShift(均为FK' s)存储员工的工资单号码,例如123456。

员工详细信息存储在TeamMembers表中(见下文)。 TeamMembers table

TeamMembers表中的用户名是员工的工资单号。

我的问题是,我似乎无法在rotas表中获取当前输入的值以显示在视图的下拉列表中。它只显示所有下拉列表中选择列表中的第一个员工(Model.TeamMembers)。 我的问题是,如何在视图的每个下拉列表中显示当前值? 我确实试过了

    @Html.DropDownListFor(m => m.Rotas[i].MorningShift, Model.TeamMembers,
   Model.Rotas[i].TeamMember1.FirstName + " " + Model.Rotas[i].TeamMember1.Surname, null)

虽然这显示了正确的员工,但它没有为该选项设置值,并且显然再次显示员工的姓名,因为它在选择列表中。

到目前为止我所拥有的:

控制器:

    public async Task<ActionResult> ModifyRotas(DateTime? dateFrom,
            DateTime? dateTo, int? Team)
    {
        Auth auth = new Auth();
        bool isAdmin = auth.IsAdmin();

        if (!isAdmin)
        {
            return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
        }

        if (dateFrom != null && dateTo != null)
        {
            DateTime today = DateTime.Today;
            DateTime startDate = Convert.ToDateTime(dateFrom);
            DateTime endDate = Convert.ToDateTime(dateTo);

            // Make sure earlier shifts can't be modified
            if (startDate < today)
            {
                startDate = today;
            }


            // Verify the team exists
            Team teamDetails = await db.Teams.FindAsync(Team);
            if (teamDetails == null)
            {
                return HttpNotFound();
            }

            ModifyRotaVM vm = new ModifyRotaVM();

            // Get rotas for the team for the dates specified
            var rotas = await db.Rotas.Where(r => r.TeamID == Team && (r.ShiftDate >= startDate && r.ShiftDate <= endDate)).ToListAsync();

            vm.Rotas = rotas;

            // Generate the dropdown lists
            var teamMembers = db.TeamMembers.Where(m => m.TeamID == Team && m.Deleted == 0).Select(m => new SelectListItem() { Text = m.FirstName + " " + m.Surname, Value = m.Username });
            ViewBag.MorningShift = teamMembers;
            ViewBag.EveningShift = teamMembers;

            vm.TeamMembers = teamMembers;

            return View(vm);

        }

        // If we got here, something went wrong; redisplay the form
        return RedirectToAction("Modify");
    }

VM:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace TeamRota.Models
    {
       public class ModifyRotaVM
       {
          public List<Rota> Rotas { get; set; }

         public IEnumerable<SelectListItem> TeamMembers { get; set; }
       }
    }

观点:

    @model TeamRota.Models.ModifyRotaVM

    @{
       ViewBag.Title = "ModifyRotas";
    }

    <h2>Modify Rotas</h2>

    @using (Html.BeginForm("RotaMod", "Rotas", new { area = "Admin" },
    FormMethod.Post))
    {
    // Being passed in so we can redirect back to the same page after the update
    @Html.Hidden("startDate", Request.QueryString["dateFrom"])
    @Html.Hidden("endDate", Request.QueryString["dateFrom"])
    @Html.Hidden("TeamID", Request.QueryString["Team"])

    <table width="100%">
       <tr>
          <td>Shift date</td>
          <td>Morning shift</td>
          <td>Evening shift</td>
      </tr>

      @for (int i = 0; i < Model.Rotas.Count(); i++)
      {

         <tr>
            <td>
               @Html.HiddenFor(m => m.Rotas[i].ID)
               @Html.DisplayFor(m => m.Rotas[i].ShiftDate)
            </td>
            <td>@Html.DropDownListFor(m => m.Rotas[i].MorningShift, Model.TeamMembers, Model.Rotas[i].TeamMember1.FirstName + " " + Model.Rotas[i].TeamMember1.Surname, null)</td>
        <td>@Html.DropDownListFor(m => m.Rotas[i].EveningShift, Model.TeamMembers, Model.Rotas[i].TeamMember.FirstName + " " + Model.Rotas[i].TeamMember.Surname, null)</td>
    </tr>
    }

    <tr>
        <td colspan="4"><input type="submit" class="btn btn-success" value="Update" /></td>
    </tr>
    </table>

    }

非常感谢任何人都可以提供的任何帮助。

感谢。

汤姆

1 个答案:

答案 0 :(得分:0)

我认为您应该尝试为DropDownListFor重新创建SelectList。

var teamMembers = db.TeamMembers.Where(m => m.TeamID == Team && m.Deleted == 0).Select(m => new { Text = m.FirstName + " " + m.Surname, Value = m.Username });
ViewBag.TeamMembers = teamMembers

然后在您看来,您必须这样做:

@Html.DropDownListFor(m => m.Rotas[i].MorningShift, new SelectList(ViewBag.TeamMembers, "Value", "Text", Model.Rotas[i].TeamMember1.Username), null)

@Html.DropDownListFor(m => m.Rotas[i].EveningShift, new SelectList(ViewBag.TeamMembers, "Value", "Text", Model.Rotas[i].TeamMember.Username), null)