C#MVC ViewModel一个发布的结果绑定,而其他人是空的

时间:2017-11-03 10:12:59

标签: c# sql asp.net post model-view-controller

我认为我有多个奖项,每个奖项中都有相应的资格列表。我创建了一个ViewModel来显示每个奖项,只需点击一个按钮,就会出现一个模态,其中包含相关的资格,可以由用户标记为已完成/更新。但是在数据的帖子上只有我的第一个奖项将在我的控制器方法中绑定。当我在VS中调试时,其余的将返回Null。数据显示在我看来如预期,每个奖项仅显示其相关资格。在此先感谢您的帮助。

视图模型

public class CandidateExtended
{
public CandidateExtended()
{
    this.Qualifications = new List<Qualification_Extended>();
}

public int AwardID { get; set; }
public int FrameworkID { get; set; }
public string ULN { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string TitleShort { get; set; }
public string TitleFull { get; set; }
public DateTime DOB { get; set; }
public string Award { get; set; }
public int AwardLevel { get; set; }
public string Status { get; set; }
public string Completion { get; set; }
public string SelectedRoute { get; set; }

public List<Qualification_Extended> Qualifications { get; set; }

public void addQualification(Qualification_Extended qualification)
{
    Qualifications.Add(qualification);
}
}

控制器

[HttpGet]
public ActionResult Index()
{

    var awardDetails = (from award in db.award
                        join candidate in db.candidate
                        on award.ULN equals candidate.ULN
                        join framework in db.framework
                        on award.QAN equals framework.QAN
                        where award.OrganisationIdentityID == organisationID
                        select new AwardDetails_Extended
                        {
                            AwardID = award.AwardID,
                            ULN = award.ULN,
                            AwardStatus = award.AwardStatus,
                            Forename = candidate.Forename,
                            Surname = candidate.Surname,
                            DOB = candidate.DOB,
                            FrameworkID = framework.FrameworkID,
                            TitleFull = framework.TitleFull,
                            TitleShort = framework.TitleShort, 
                            AwardLevel = framework.AwardLevel, 
                            Award = framework.Award,
                            Completion = framework.Completion
                        }).ToList();


    var qualificationDetails = (from candidateQualification in db.candidateQualification
                                join qualification in db.qualification
                                on candidateQualification.QualificationID equals qualification.QualificationID
                                select new Qualification_Extended
                                {
                                    ID = candidateQualification.ID,
                                    QualificationID = candidateQualification.QualificationID,
                                    ULN = candidateQualification.ULN,
                                    FrameworkID = candidateQualification.FrameworkID,
                                    Achieved = candidateQualification.Achieved,
                                    DateAchieved = candidateQualification.DateAchieved
                                }).ToList();


    List<CandidateExtended> candidateVM = new List<CandidateExtended>();

    foreach (var item in awardDetails)
    {
        CandidateExtended vm = new CandidateExtended();
        vm.AwardID = item.AwardID;
        vm.FrameworkID = item.FrameworkID;
        vm.ULN = item.ULN;
        vm.Forename = item.Forename;
        vm.Surname = item.Surname;
        vm.DOB = item.DOB;
        vm.TitleShort = item.TitleShort;
        vm.TitleFull = item.TitleFull;
        vm.Award = item.Award;
        vm.AwardLevel = item.AwardLevel;
        vm.Status = item.AwardStatus;
        vm.Completion = item.Completion;
        vm.SelectedRoute = item.SelectedRoute;

        foreach (var qualification in qualificationDetails)
        {
            if (qualification.ULN == item.ULN && qualification.FrameworkID == item.FrameworkID)
            {
                vm.addQualification(qualification);
            }
        }

        candidateVM.Add(vm);
    }

    return View(candidateVM);      
}

查看

@model List<Apprenticeship_SingleAward.ViewModels.CandidateExtended>

@{
  ViewBag.Title = "Single Award Apprenticeships";
  Layout = "~/Views/Shared/_Organisation.cshtml";
}


<div class="application-table table-responsive">
    <table class="table table-striped administration-table">
        <thead>
            <tr class="admin-table-heading">
                <th>ULN</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Title</th>
                <th>Award</th>
                <th>Level</th>
                <th>Qualifications</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
        @for (int j = 0; j < Model.Count(); j++)
        {
            var award = Model[j];

            <tr>
                <td>@award.ULN</td>
                <td>@award.Forename</td>
                <td>@award.Surname</td>
                <td>@award.TitleShort</td>
                <td>@award.Award</td>
                <td>@award.AwardLevel</td>
                <td>
                    <button class="btn qualificationbutton" data-toggle="modal" data-target="#@qualificationModal">@displayQualTotal<i class="glyphicon glyphicon-pencil org-edit-icon"></i></button>

                    @using (Html.BeginForm("UpdateAward", "Organisation", FormMethod.Post, new { id = award.AwardID}))
                    {
                        @Html.HiddenFor(a => Model[j].AwardID)

                        <div id="@qualificationModal" class="modal fade" role="dialog">
                            <div class="modal-dialog organisationmodal">
                                <div class="modal-content">
                                  <div class="modal-body">
                                      <h4 class="org-modal-subheading">@award.TitleShort</h4>
                                      <br />
                                      <div class="row">
                                             <div class="col-md-12">
                                                <div class="row org-row-main">
                                                    <div class="col-md-7"><h4 class="org-type">Qualification</h4></div>
                                                    <div class="col-md-2"><h5 class="org-completed">Completed</h5></div>
                                                    <div class="col-md-3"><h5 class="org-date">Date</h5></div>
                                                </div>

                                                <hr class="org-hr"/>

                                                 @for (int i = 0; i < Model[j].Qualifications.Count(); i++)
                                                 {
                                                    <div class="row org-row">
                                                       <div class="col-md-7">
                                                           @Html.HiddenFor(a => Model[j].Qualifications[i].ID)
                                                        </div>
                                                        <div class="col-md-2">
                                                           @Html.CheckBoxFor(a => Model[j].Qualifications[i].Achieved)
                                                        </div>
                                                        <div class="col-md-3"
                                                            >@Html.TextBoxFor(a => Model[j].Qualifications[i].DateAchieved, "{0:dd/MM/yyyy}")
                                                        </div>
                                                   </div>
                                                 }
                                             </div>
                                       </div> 
                                  </div>
                                  <div class="modal-footer">
                                    <button type="button" class="btn ccea-signout" data-dismiss="modal">Close</button>
                                    <button type="submit" class="btn admin-button" style="margin-top: 0;">Save</button>
                                  </div>
                                </div>
                            </div>
                        </div>
                    }
                </td>
                <td>
                    @{
                    var status = award.Status;
                    if (status == "In Progress")
                    {
                        <button class="btn progressbutton" style="margin: 0;">@status</button>
                    }
                    }
               </td>
            </tr>

        }

      </tbody>
  </table>

UpdateAward

[HttpPost]
public ActionResult UpdateAward(List<CandidateExtended> Model)
{

    return RedirectToAction("Index", "Login");

}

1 个答案:

答案 0 :(得分:1)

现在设置的方式(BeginForm @for,每个'迷你'表单的提交按钮),每次提交都会执行表单帖子中只包含列表中的一个项目。

如果这是您希望它的工作方式(一次编辑一个项目),那么您可以保留它。但请记住(添加注释?)即使Post Action方法准备好来接收List,List中也不会有多个项目,因为每个'mini'-Form只包含一件事。

另一方面,如果您希望能够一次保存多个项目,请将BeginForm 移到<{strong>外面 @for,然后只使用一个“提交”按钮表格的结尾,例如就在结束}之前。