I have a MVC form which is more complex than all of my others, utilising three models.
Company -> Base_IP -> RequestedIP
which goes ViewModel -> Partial1 -> Partial2
I am using BeginCollectionItem for this has each model has a property list of the the model down from it. IE - Company has a property called baseIps, the BaseIp class has a property called requestedIps, it is requestedIps
that is coming back null, the count is there on page render, but is not on submit.
When submitting to the database in the post
Create(), I get nulls on the 'requestedIps' property, why is this?
I've added the offending controller and partial code samples below, not the entire thing as it's massive/redundant - any questions, please let me know.
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);
}
Create 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" }
}
RequestedIpView
@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>