C#MVC ViewModel一个发布的结果绑定,而其他则为空 [英] C# MVC ViewModel One Posted Result Binds Whilst Others Are Null

查看:94
本文介绍了C#MVC ViewModel一个发布的结果绑定,而其他则为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我有很多奖项,并且每个奖项中都有相应的资格列表.我创建了一个ViewModel来显示每个奖项,并单击一个按钮,应该显示一个模态及其相关的限定条件,可以将其标记为由用户完成/更新.但是在数据发布中,只有我的第一个Award会绑定到我的控制器方法中,其余的会在我在VS中调试时以Null的形式返回.我认为这些数据与预期的一样出现,每个奖项仅显示其相关资格.在此先感谢您的帮助.

I have a number of awards in my view and within each award there is a corresponding list of qualifications. I have created a ViewModel to display each award and with a click of a button a modal should appear with its relevant qualifications which can be marked as completed/updated by the user. However on the Post of the data only my first Award will bind in my controller method.The rest will comeback as Null when I debug in VS. The data is appearing in my view as expected with each Award only showing its relevant qualifications. Thanks in advance for helping with this.

ViewModel

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");

}

推荐答案

现在设置的方式(在BeginForm 内部 @for中,以及每个'mini '-Form),每个提交者都会进行一次表单发布,其中仅包含列表中的一项.

The way it is set up now (with BeginForm inside the @for, and a Submit button for every 'mini'-Form), every submit will do a Form Post containing exactly one item from the list.

如果这是您希望其工作的方式(一次编辑一项),则可以保留它.但是请记住(添加评论?),即使Post Action方法已经准备好接收列表,但列表中永远不会有多个项目,因为每个迷你"表单仅包含一个项目.

If that is how you want it to work (edit one item at a time), then you may keep it. Do keep in mind however (add a comment?) that even though the Post Action method is ready to receive a List, there never will be multiple items in the List because each 'mini'-Form contains just one item.

另一方面,如果您希望一次保存多个项目,则将BeginForm 移到@for,然后仅使用一个提交按钮即可.形式,例如就在结束}之前.

If on the other hand you want to be able to save multiple items at once, then move the BeginForm outside the @for, and use just one Submit button at the end of the Form, e.g. just before the closing }.

这篇关于C#MVC ViewModel一个发布的结果绑定,而其他则为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆