gpt4 book ai didi

c# - 部分中的 BeginCollectionItem 部分行为不正确

转载 作者:行者123 更新时间:2023-11-30 16:52:35 25 4
gpt4 key购买 nike

我正在尝试通过 MVC 5 中的表单提交一次性绑定(bind)我所有的模型数据,使用 Joe Steven 的博客 here 中讨论的 BeginCollectionItem 编辑版本。 .

模型,Company , 有一个 List<Pa_Ipv4> , 类 Pa_Ipv4反过来有一个List<IpAllocation> ,我想访问 IpAllocation 的所有属性并将其保存到数据库中在每个Pa_Ipv4 .

即:Model.pa_ipv4s[x].requestedIps[x].subnet

主页正在使用模型Company ,它有一个部分接受 Pa_Ipv4 ,它有一个部分接受 IpAllocation .

问题 1: 在我的 Controller 中,我为列表中的第一项 (requestedIp) 设置了一个字符串属性,但是当我提交和回发时,该属性 ( allocationType) 是null,此属性需要进行硬编码,因为它供数据库内部使用 - 为什么要重置此属性?

原因:该属性不在 post 方法中,因为最初声明的内容被丢弃,因为它不在结束 post 中。

可能的解决方案:在表单中使用一个隐藏属性,以便它在发布表单时出现并且用户无法访问该属性。

问题 2:BeginCollectionItem 正确命名属性,IE:pa_ipv4s[8e075d50-a5fb-436f-9cef-85abfb6910e3].requestedIps[b693b83c-b6b1-4c42-b983-4d058e766d4c].subnet ,但只有初始模型,然后忽略任何其他创建的模型,我做错了什么?

原因:Pa_Ipv4 部分 BeginCollectionItem 生成的前缀所需的 GUID 无法被 IpAllocation BeginCollectionItem 访问,因此只有初始内容具有正确的前缀,此后添加的任何内容都会丢失必要的前缀。

另一个潜在的解决方案本质上是相同的概念,但不是使用 div,而是使用 html 数据属性,以便它可以访问。

我认为我遇到的两个问题都与我如何设置 Controller 有关,但我在下面也包括了 View 和模型。该模型包含所有属性,在我的 View 中已删除其中许多属性以节省空间,因为这些属性不会导致问题。

创建

@model Company

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
<div class="jumboservice">
<div data-role="page">
<div data-role="header">
<h2>PA IPv4 Request Form</h2>
</div>
<div class="ui-content" data-role="main">
<h3>Company Details</h3>
<div class="ui-grid-c ui-responsive">
<div class="ui-block-a">
<p class="lblStyle">Company Name</p>
<span>
@Html.EditorFor(m => m.name)
@Html.ValidationMessageFor(m => m.name)
</span>
</div>
</div>
</div>
<br />
@foreach (var i in Model.pa_ipv4s)
{
@Html.Partial("Pa_IPv4View", i)
}
<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>
</div>
</div>
}
<script type="text/javascript">
$(function () {
$('#addItemRIpM').on('click', function () {
$.ajax({
url: '@Url.Action("RequestedManager")',
cache: false,
success: function (html) { $("#editorRowsRIpM").append(html); }
});
return false;
});
$('#editorRowsRIpM').on('click', '.deleteRow', function () {
$(this).closest('.editorRow').remove();
});
});
</script>

Pa_Ipv4 部分

@model Pa_Ipv4

@using (HtmlHelpers.BeginCollectionItem.HtmlPrefixScopeExtensions.BeginCollectionItem(Html,"pa_ipv4s"))
{
@Html.AntiForgeryToken()
<div class="ui-grid-c ui-responsive">
<div class="ui-block-a">
<p class="lblStyle">Subnet</p>
</div>
<div class="ui-block-b">
<p class="lblStyle">Size(CIDR)</p>
</div>
<div class="ui-block-c">
<p class="lblStyle">Mask</p>
</div>
<div class="ui-block-d">
</div>
</div>
@*Request IP Address Space List*@
<div id="editorRowsRIpM">
@foreach (var item in Model.requestedIps)
{
@Html.Partial("RequestedIpView", item)
}
</div>
@Html.ActionLink("Add", "RequestedManager", null, new { id = "addItemRIpM", @class = "button" })
}

RequestedIp 部分

@model IpAllocation

<div class="editorRow">
@using (HtmlHelpers.BeginCollectionItem.HtmlPrefixScopeExtensions.BeginCollectionItem(Html, "requestedIps"))
{
<div class="ui-grid-c ui-responsive">
<div class="ui-block-a">
<span>
@Html.TextBoxFor(m => m.subnet)
</span>
</div>
<div class="ui-block-b">
<span>
@Html.TextBoxFor(m => m.cidr)
</span>
</div>
<div class="ui-block-c">
<span>
@Html.TextBoxFor(m => m.mask)
<span class="dltBtn">
<a href="#" class="deleteRow">Remove</a>
</span>
</span>
</div>
</div>
}
</div>

Controller

public ActionResult Create()
{
var cmp = new Company();
cmp.contacts = new List<Contact>
{
new Contact { email = "", name = "", telephone = "" }
};
cmp.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 = "" } // allocationType is null in cmp in the Create[HttpPost]
}
}
};

return View(cmp);
}

public ActionResult Pa_IPv4Manager()
{
return PartialView("Pa_IPv4View", new Pa_Ipv4());
}
public ActionResult RequestedManager()
{
return PartialView("RequestedIpView", new IpAllocation { allocationType = "Requested" }); // allocationType is null in cmp in the Create[HttpPost]
}

// POST: Pa_Ipv4/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Company cmp) //only one requestedIps count regardless of how many add
{
if (ModelState.IsValid)
{
db.companys.Add(cmp);
db.SaveChanges();
return RedirectToAction("Index");
}

型号

[Table("Ipv_Base")]
public class Ipv_Base
{
[Key]
public int ipv_baseId { get; set; }
public int companyId { get; set; }
[ForeignKey("companyId")]
public Company company { get; set; }
public string ipType { get; set; }
[Required]
public bool registedAddress { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string existingNotes { get; set; }
[Required]
public int numberOfAddresses { get; set; }
[Required]
public bool returnedAddressSpace { get; set; }
[DataType(DataType.MultilineText)]
public string additionalInformation { get; set; }
// navigation properties
public virtual IList<IpAllocation> requestedIps { get; set; }
}
[Table("Company")]
public class Company
{
[Key]
public int companyId { get; set; }
[Required]
public string name { get; set; }
[Required]
public string telephone { get; set; }
[Required]
public string regNumber { get; set; }
// navigation properties to keep track of the models that belong to the company
public virtual IList<Pa_Ipv4> pa_ipv4s { get; set; }

}
[Table("IpAllocation")]
public class IpAllocation
{
[Key]
public int ipAllocationId { get; set; }
public int ipv_BaseId { get; set; }
[ForeignKey("ipv_BaseId")]
public Ipv_Base ipv_Base { get; set; }
[Required]
public string allocationType { get; set; }
[Required]
public string subnet { get; set; }
[Required]
public string cidr { get; set; }
[Required]
public string mask { get; set; }
}
public class Pa_Ipv4 : Ipv_Base
{
public Pa_Ipv4()
{
ipType = "pa_ipv4";
}
}

最佳答案

问题1解决方案:

Q1 的问题是我在 Controller 中分配的属性值没有从表单提交中解析回来,因为该属性不存在。

为属性添加了一个隐藏字段以纠正讨厌的 null。

<div class="ui-block-a">
<span>
@Html.HiddenFor(m => m.allocationType)
@Html.TextBoxFor(m => m.subnet, new { @class = "checkFiller" })
</span>
</div>

问题2解答:

当我将第一个模型的 GUID 作为前缀附加到第二个模型时,我遇到的问题主要是由于我如何使用 AJAX 将数据发送到 Controller 操作方法。

下面显示的代码片段修复了问题并显示了正确绑定(bind)的 GUID。

name="pa_ipv4s[f7d8d024-5bb6-451d-87e3-fd3e3b8c1bba].requestedIps[d5c08a43-f65e-46d1-b224-148225599edc].subnet" 现在显示在动态创建的模型上属性,而不仅仅是最初创建的。

当在 Visual Studio 中调试运行并将鼠标悬停在模型上时,深入挖掘数据会显示模型列表的正确计数。

Controller Action 方法:

public ActionResult ExistingManager(string containerPrefix)
{
ViewData["ContainerPrefix"] = containerPrefix;
return PartialView("ExistingIpView", new IpAllocation { allocationType = "Existing" });
}

AJAX GET 方法调用 Controller ActionMethod:

$('#addItemEIpM').on('click', function () {
$.ajax({
url: '@Url.Action("ExistingManager")',
cache: false,
data: 'containerPrefix=' + $('#addItemEIpM').data('containerprefix'),
success: function (html) {
$("#editorRowsEIpM").append(html);
}
});
return false;
});

关于c# - 部分中的 BeginCollectionItem 部分行为不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412960/

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