gpt4 book ai didi

c# - 如何将包含 IEnumerable 模型(复杂)的模型从 View C# MVC3 传递到 Controller ?

转载 作者:太空狗 更新时间:2023-10-29 22:53:23 27 4
gpt4 key购买 nike

我浏览了该站点上的其他问题和答案,但找不到我需要的答案。

我有一个 StudentRecord 实体:

public class StudentRecord : Persistent {
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual DateTime Dob { get; set; }
public virtual int StudentRef { get; set; }
public virtual IEnumerable<StudentAddress> Addresses { get; set; }
public virtual StudentAddress Address { get; set; }
public virtual string Notes { get; set; }
}

如您所见,它包含一个 StudentAddress 实体以及一个 StudentAddress 的 IEnumerable:

public class StudentAddress: Persistent {
public virtual int StudentRef { get; set; }
public virtual string Addressee { get; set; }
public virtual string Property { get; set; }
public virtual string District { get; set; }
public virtual string PostalTown { get; set; }
public virtual string County { get; set; }
public virtual string Postcode { get; set; }
}

我正在将学生记录传递给包含在 View 模型中的 View :

public class UserViewModel {
public StudentRecord Student;
public ICurrentUserService CurrentUserService;
public ParentUser ParentUser;
}

然后将其显示在表单中以便对其进行编辑,提交表单会将 StudentRecord 传回 Controller 。除 StudentRecord 中的地址为空外,一切正常。 StudentRecord 中的单个 StudentAddress 用于添加新地址,并且也可以正常工作。

是否可以编辑地址并将其发送回 Controller ,或者我是否需要将它们以单独的形式放在单独的页面上?我可以做到这一点,但更愿意将所有功能合二为一。

我的问题可能是这不可能,或者可能是我将地址放入表单的方式。一名学生可能有多个地址。

这是表格:(为了清楚起见,我删除了一些 html 布局。“添加另一个地址”复选框显示了带有 jquery 的新学生地址部分。)

@using (Html.BeginForm()) {
Personal Details
Full Name: @Html.TextBoxFor(x => x.Student.FirstName) @Html.TextBoxFor(x => x.Student.LastName)
DOB: @Html.TextBoxFor(x => x.Student.Dob)

@if (Model.Student.Addresses.Any()) {
// Only print addresses if they exist
int count = 1;
int element = 0;
@if (Model.Student.Addresses.Count() > 1) {
foreach (var address in Model.Student.Addresses) {
Student Address @count
Addressee @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Addressee)
Property @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Property)
District @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).District)
Postal Town @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).PostalTown)
County @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).County)
Postcode @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(element).Postcode)
count++;
element++;
} //end foreach
} else {
Student Address
Addressee @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Addressee)
Property @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Property)
District @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).District)
Postal Town @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).PostalTown)
County @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).County)
Postcode @Html.TextBoxFor(x => x.Student.Addresses.ElementAt(0).Postcode)
} @*end if (Model.Student.Addresses.Count() > 1)*@

Add another address @Html.CheckBox("Add another address", false, new {@id = "newBox"})

New Student Address
Addressee @Html.TextBoxFor(x => x.Student.Address.Addressee)
Property @Html.TextBoxFor(x => x.Student.Address.Property)
District @Html.TextBoxFor(x => x.Student.Address.District)
Postal Town @Html.TextBoxFor(x => x.Student.Address.PostalTown)
County @Html.TextBoxFor(x => x.Student.Address.County)
Postcode @Html.TextBoxFor(x => x.Student.Address.Postcode)
} else {
No address for this student.
} @*end if (Model.Student.Addresses.Any())*@

Notes: @Html.TextAreaFor(x => x.Student.Notes, new { @style = "width: 100%;"})

<input type="submit" value="Send" class="btn btn-primary" style="clear: both;"/>
} @*end of form*@

最佳答案

问题是文本输入控件的 name 属性不包含正确的值。我邀请您阅读 following blog post更好地理解默认模型绑定(bind)器用于绑定(bind)到集合和字典的约定。

然后我建议您使用编辑器模板而不是在您的 View 中编写 foreach 循环:

@using (Html.BeginForm()) {
Personal Details

@Html.LabelFor(x => x.Student.FirstName, "Full Name:")
@Html.EditorFor(x => x.Student.FirstName)
@Html.EditorFor(x => x.Student.LastName)

@Html.LabelFor(x => x.Student.Dob, "DOB:")
@Html.TextBoxFor(x => x.Student.Dob)

@if (Model.Student.Addresses.Any()) {
@Html.EditorFor(x => x.Student.Addresses)
} else {
<text>No address for this student.</text>
}

@Html.LabelFor(x => x.Student.Notes, "Notes:")
@Html.TextAreaFor(x => x.Student.Notes, new { @style = "width: 100%;"})

<input type="submit" value="Send" class="btn btn-primary" style="clear: both;"/>
}

然后定义一个自定义编辑器模板,该模板将为地址集合的每个元素自动呈现 (~/Views/Shared/EditorTemplates/StudentAddress.cshtml):

@model StudentAddress
@Html.LabelFor(x => x.Addressee, "Addressee")
@Html.EditorFor(x => x.Addressee)

@Html.LabelFor(x => x.Property, "Property")
@Html.EditorFor(x => x.Property)

@Html.LabelFor(x => x.District, "District")
@Html.EditorFor(x => x.District)

@Html.LabelFor(x => x.PostalTown, "Postal Town")
@Html.EditorFor(x => x.PostalTown)

@Html.LabelFor(x => x.County, "County")
@Html.EditorFor(x => x.County)

@Html.LabelFor(x => x.Postcode, "Postcode")
@Html.EditorFor(x => x.Postcode)

但这一切都是静态的。如果您希望能够动态添加和删除地址,我邀请您阅读 following blog post来自 Steven Sanderson,他在其中说明了如何使用自定义 HTML 帮助程序为输入字段生成正确的名称 (Html.BeginCollectionItem) 并使用 AJAX 添加新行。

关于c# - 如何将包含 IEnumerable 模型(复杂)的模型从 View C# MVC3 传递到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798823/

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