gpt4 book ai didi

C# MVC ViewModel 返回 Null - 回传

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:16 24 4
gpt4 key购买 nike

在我看来,我有很多奖项,每个奖项中都有相应的资格列表。我创建了一个 ViewModel 来显示每个奖项,单击一个按钮就会出现一个模态及其相关资格,用户可以将其标记为已完成/更新。但是,在发布数据时,它没有绑定(bind)到我的 Controller 方法中的 ViewModel。数据在我看来如预期般出现,每个奖项仅显示其相关资格。我已使用 FormCollection 访问某些字段以进行测试,并且正在回发数据。任何帮助都会很棒!

View 模型

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

Controller

  [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);
}

查看

@using (Html.BeginForm("UpdateAward", "Organisation", FormMethod.Post))
{
@Html.HiddenFor(a => award.AwardID)

<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 < award.Qualifications.Count(); i++)
{

var qualification = award.Qualifications[i];

<div class="row org-row">
<div class="col-md-7">
@Html.HiddenFor(a => award.Qualifications[i].ID)
</div>
<div class="col-md-2">
@Html.CheckBoxFor(a => award.Qualifications[i].Achieved)
</div>
<div class="col-md-3">@Html.TextBoxFor(a => award.Qualifications[i].DateAchieved, "{0:dd/MM/yyyy}")
</div>
</div>
}
</div>
</div>

<button type="submit" class="btn admin-button" style="margin-top: 0;">Save</button>

}

更新奖

[HttpPost]
public ActionResult UpdateAward(CandidateExtended model, FormCollection collection)
{

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

}

最佳答案

首先(你可能已经有了这个,但我们看不到):你的 View 应该以包含 @model List<CandidateExtended> 的行开始(在内部 Type 前加上适当的命名空间)。

然后在 View 中你应该使用Model ,这是在 @model 之后指定的确切类型的定义关键字。

我们看到您正在使用 award ,我们看不到它来自哪里,大概是使用类似 var award = Model[j] 的东西设置的或 foreach (var award in Model) .
永远不要在 View 中使用此类临时变量或辅助变量(为了提高效率)来呈现表单; View 需要所有对象的完全限定名称,例如Model.Item[x].SubItem[y] 以生成可用于模型绑定(bind)的表单字段名称

例如这:@Html.HiddenFor(a => award.Qualifications[i].ID)
应该是:@Html.HiddenFor(a => Model[j].Qualifications[i].ID)

并在所有其他地方进行相同的更改。

然后按照已经建议的那样做,使用List<...>在你的 Controller Post 方法中。

最后还请删除FormCollection ,如果您按照此处所述设置了所有内容,则不需要。体面的 MVC 代码从不使用FormCollection , 曾经。

关于C# MVC ViewModel 返回 Null - 回传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47079764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com