gpt4 book ai didi

c# - 部分在部分空异常中

转载 作者:行者123 更新时间:2023-11-30 21:55:24 25 4
gpt4 key购买 nike

我有一个 MVC 表单,它比我所有的其他表单都复杂,它使用了三个模型。

Company -> Base_IP -> RequestedIP这是ViewModel -> Partial1 -> Partial2

我正在使用 BeginCollectionItem,因为每个模型都有一个模型的属性列表。 IE - 公司有一个名为 baseIps 的属性,BaseIp 类有一个名为 requestedIps 的属性,它是 requestedIps返回 null,计数在页面呈现时存在,但不在提交时。

post提交到数据库时Create(),我在“requestedIps”属性上得到空值,这是为什么?

我在下面添加了有问题的 Controller 和部分代码示例,而不是全部,因为它很大/冗余 - 如有任何问题,请告诉我。

Controller - [HttpGet]Create()

public ActionResult Create()
{
var cmp = new Company
{
contacts = new List<Contact>
{
new Contact { email = "", name = "", telephone = "" }
}, pa_ipv4s = new List<Pa_Ipv4>
{
new Pa_Ipv4
{
ipType = "Pa_IPv4", registedAddress = false, existingNotes = "", numberOfAddresses = 0, returnedAddressSpace = false, additionalInformation = "",
requestedIps = new List<IpAllocation>
{
new IpAllocation { allocationType = "Requested", cidr = "", mask = "", subnet = "" }
}
}
}
};
return View(cmp);
}

Controller - [HttpPost]Create()

        [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Company cmp) // does not contain properties assigned/added to in view render
{
if (ModelState.IsValid)
{
db.companys.Add(cmp);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(cmp);
}

创建 View

@model Company
@using (Html.BeginForm())
{
<div id="editorRowsAsn">
@foreach (var ip in Model.pa_ipv4s)
{
@Html.Partial("Pa_IPv4View", ip)
}
</div>
<br />
<div data-role="main" class="ui-content">
<div data-role="controlgroup" data-type="horizontal">
<input type="submit" class="ui-btn" value="Create" />
</div>
</div>
}

Pa_Ipv4 View

@model Pa_Ipv4
@using (Html.BeginCollectionItem("pa_ipv4s"))
{
@Html.AntiForgeryToken()

<div id="editorRowsRIpM">
@foreach (var item in Model.requestedIps)
{
@Html.Partial("RequestedIpView", item)
}
</div>
@Html.ActionLink("Add", "RequestedManager", null, new { id = "addItemRIpM", @class = "button" }

}

请求的IpView

@model IpAllocation
<div class="editorRow">
@using (Html.BeginCollectionItem("requestedIps"))
{
<div class="ui-grid-c ui-responsive">
<div class="ui-block-a">
<span>
@Html.TextBoxFor(m => m.subnet, new { @class = "checkFiller" })
</span>
</div>
<div class="ui-block-b">
<span>
@Html.TextBoxFor(m => m.cidr, new { @class = "checkFiller" })
</span>
</div>
<div class="ui-block-c">
<span>
@Html.TextBoxFor(m => m.mask, new { @class = "checkFiller" })
<span class="dltBtn">
<a href="#" class="deleteRow"><img src="~/Images/DeleteRed.png" style="width: 15px; height: 15px;" /></a>
</span>
</span>
</div>
</div>
}
</div>

最佳答案

您的第一个(外部)部分将生成与您的模型相关的正确名称属性(您的代码未在 Pa_Ipv4.cshtml View 中显示任何控件,但我假设您确实有一些),例如

<input name="pa_ipv4s[xxx-xxx].someProperty ...>

但是内部部分不会因为 @using (Html.BeginCollectionItem("requestedIps")) 会生成

<input name="requestedIps[xxx-xxx].subnet ...>
<input name="requestedIps[xxx-xxx].cidr ...>

他们应该在的地方

<input name="pa_ipv4s[xxx-xxx].requestedIps[yyy-yyy].subnet ...>
<input name="pa_ipv4s[xxx-xxx].requestedIps[yyy-yyy].cidr ...>

通常您可以使用额外的 View 数据将前缀传递给部分(参见 this answer 的示例),但不幸的是,您无权访问 生成的 Guid BeginCollectionItem 帮助程序,因此无法正确地为 name 属性添加前缀。

文章herehere讨论创建您自己的助手来处理嵌套集合。

其他选项包括使用嵌套的 for 循环和包含集合索引器的隐藏输入,这将允许您从集合中删除项目并且在提交表单时仍然能够绑定(bind)到您的模型。

for (int i = 0; i < Model.pa_ipv4s.Count; i++)
{
for(int j = 0; j < Model.pa_ipv4s[i].requestedIps.Count; j++)
{
var name = String.Format("pa_ipv4s[{0}].requestedIps.Index", i);
@Html.TextBoxFor(m => m.pa_ipv4s[i].requestedIps[j].subnet)
@Html.TextBoxFor(m => m.pa_ipv4s[i].requestedIps[j].cidr)
...
<input type="hidden" name="@name" value="@j" />
}
}

但是,如果您还需要动态添加新项目,则需要使用 javascript 生成 html(引用示例 herehere)

关于c# - 部分在部分空异常中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335703/

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