我有PROPERTY
VM,其中包含List<FounderInvestmentViewModel>
。我已成功将FounderInvestmentViewModel
的局部视图插入到Main Create Property视图中。
FounderInvestmentViewModel反过来包含List<InstallmentDetailsViewModel>
。我创建了InstallmentDetailsViewModel
的部分视图作为_InstallmentDetails.cshtml
和所有必要的操作。
我想将_InstallmentDetails.cshtml
插入FounderInvestmentViewModel
的局部视图中,然后将其插入主视图中。
首先让我们看一下目前使用的代码: -
物业视图型号: -
public class PropertyViewModel
{
public int? Id { get; set; }
public string PropertyTitle { get; set; }
....other attributes....
public List<FounderInvestmentViewModel> FounderInvestments { get; set; } = new List<FounderInvestmentViewModel>();
}
FounderInvestmentViewModel: -
public class FounderInvestmentViewModel
{
public int? Id { get; set; }
public int InvestorId { get; set; }
public double Investment { get; set; }
public int InstallmentPeriod { get; set; }
public IEnumerable<SelectListItem> FounderInvestorList { get; set; }
public List<InstallmentDetailsViewModel> InstallmentDetails { get; set; } = new List<InstallmentDetailsViewModel>();
}
分期付款细节查看型号: -
public class InstallmentDetailsViewModel
{
public int? Id { get; set; }
[Display(Name = "Pay Date")]
public List<DateTime> PayDates { get; set; }
[Required]
public List<double> InstallmentAmounts { get; set; }
}
分期详情的部分视图(_ Installment Details.cshtml): -
@model propertyMgmt.ViewModel.InstallmentDetailsViewModel
<div class="installmentDetails">
@using (Html.BeginCollectionItem("InstallmentDetails"))
{
@Html.HiddenFor(m => m.Id, new { @class = "id" })
<div class="form-group">
@Html.LabelFor(m => m.InstallmentAmounts, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => m.InstallmentAmounts, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
@Html.ValidationMessageFor(m => m.InstallmentAmounts, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.PayDates, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => m.PayDates, new { htmlAttributes = new { @class = "form-control", @placeholder = "01/02/2017" } })
@Html.ValidationMessageFor(m => m.PayDates, "", new { @class = "text-danger" })
</div>
</div>
}
</div>
这个_InstallmentDetails.cshtml被插入到这个_Founder Investment Details.cshtml中,这是FounderInvestmentDetails View Part Model的部分视图: -
@model propertyMgmt.ViewModel.FounderInvestmentViewModel
<div class="founderInvestmentDetails">
@using (Html.BeginCollectionItem("FounderInvestments"))
{
@Html.HiddenFor(m => m.Id, new { @class = "id" })
<div class="form-group">
@Html.LabelFor(m => m.InvestorId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.InvestorId, Model.FounderInvestorList, "Select Investor", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.InvestorId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Investment, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => m.Investment, new { htmlAttributes = new { @class = "form-control", @type = "number" } })
@Html.ValidationMessageFor(m => m.Investment, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.InstallmentPeriod, htmlAttributes: new { @class = "control-label col-md-2", @type = "number" })
<div class="col-md-10">
@Html.EditorFor(m => m.InstallmentPeriod, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => m.InstallmentPeriod, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="installmentDetailsDiv">
@foreach (var InstallmentDetails in Model.InstallmentDetails)
{
@Html.Partial("_InstallmentDetails", InstallmentDetails)
}
</div>
<div class="form-group col-md-10">
<input type="button" class="btn btn-info btn-xs" value="Add Installment Details" onclick="addInstallmentDetails()" />
</div>
}
</div>
这是主要创造观点: -
@model propertyMgmt.ViewModel.PropertyViewModel.PropertyViewModel
@{
ViewBag.Title = "Create";
}
<script src="~/Areas/Admin/themes/jquery/jquery.min.js"></script>
<h2>Property</h2>
@using (Html.BeginForm("Create", "Property", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Add Property</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PropertyTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PropertyTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PropertyTitle, "", new { @class = "text-danger" })
</div>
</div>
.....Other form Groups.....
<div id="founderInvestmentDetails">
@foreach(var FounderInvestments in Model.FounderInvestments)
{
@Html.Partial("_FounderInvestmentDetails", FounderInvestments)
}
</div>
<div class="form-group col-md-10" >
<input type="button" class="btn btn-info btn-xs" value="Add Founder Investors" onclick="addFounderInvestors()" />
</div>
</div>
}
这是主视图中的My JS代码: -
function addFounderInvestors() {
var url = '@Url.Action("FounderInvestmentDetails")';
var form = $('form');
var founders = $('#founderInvestmentDetails');
$.get(url, function (response) {
founders.append(response);
// Reparse the validator for client side validation
form.data('validator', null);
$.validator.unobtrusive.parse(form);
});
};
function addInstallmentDetails() {
var url = '@Url.Action("InstallmentDetails")';
var form = $('form');
var installments = $('#installmentDetailsDiv');
$.get(url, function (response) {
installments.append(response);
// Reparse the validator for client side validation
form.data('validator', null);
$.validator.unobtrusive.parse(form);
});
};
控制器代码: -
public PartialViewResult FounderInvestmentDetails()
{
var model = new FounderInvestmentViewModel {
FounderInvestorList = _investorQueryProcessor.GetInvestorByType(1).Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.InvestorName
})
};
//return PartialView(model);
return PartialView("_FounderInvestmentDetails", model);
}
public PartialViewResult InstallmentDetails()
{
return PartialView("_InstallmentDetails",new InstallmentDetailsViewModel());
}
public ActionResult Create()
{
if (Session["AdminName"] != null)
{
//ViewBag.Investors = SelectListItems;
List<FounderInvestmentViewModel> model = new List<FounderInvestmentViewModel>();
List<InstallmentDetailsViewModel> model2 = new List<InstallmentDetailsViewModel>();
return View(new PropertyViewModel());
}
else return Redirect("/Account/Login");
}
编辑: - 抱歉这是抛出异常 - >> Collection.cshtml
PROCESS: - 在主视图中,点击事件上的“添加方正投资者按钮”成功添加部分视图_FounderInvestmentDetails.cshtml
。现在附加“添加安装细节”按钮。点击此“添加分期付款详情”按钮,应附加_InstallmentDetails.cshtml
局部视图,但此部分不起作用。当我单击此按钮时,我在以下代码中得到错误"Object reference not set to an instance of an object"
: -
@using HtmlHelpers.BeginCollectionItem
<ul>
@foreach (object item in Model)-->>ERROR CODE
{
<li>
@using (Html.BeginCollectionItem(Html.ViewData.TemplateInfo.HtmlFieldPrefix))
{
@Html.EditorFor(_ => item, null, "")
}
</li>
}
</ul>
Paydates和Installments不需要是<List>
,因为它已经是PartialView并且可以多次添加。
public class InstallmentDetailsViewModel {
public int? Id { get; set; }
[Display(Name = "Pay Date")]
public List<DateTime> PayDates { get; set; }
[Required]
public List<double> InstallmentAmounts { get; set; }
}