繁体   English   中英

局部视图计数= 0,值未发送到控制器

[英]Partial View Count=0, value not being sent to the controller

仅部分视图表单的值未传递给控制器​​。:/ FounderInvestmentVM是我创建的部分视图的一个,该VM在PropertyVM内部。其他值传递给控制器​​,但不传递给部分视图的值。 当我放入调试器并查看它时,它始终为FounderInvestments Count=0 :/

这是我的包含FounderInvestorVM的PROPERTY VM:

namespace propertyMgmt.ViewModel.PropertyViewModel
{
    public class PropertyViewModel
    {     
        public int? Id { get; set; }
        [Required]
        [DisplayName("Property Title")]
        public string PropertyTitle { get; set; }
        ......
        public List<FounderInvestmentViewModel> FounderInvestments { get; set; }=new List<>(FounderInvestmentViewModel);
    }
}

这是FounderInvestorVM:-

public class FounderInvestmentViewModel
    {
        public int? Id { get; set; }
        public int PropertyId { get; set; }
        public int InvestorId { get; set; }
        public double Investment { get; set; }
        public int InstallmentPeriod { get; set; }
        public IEnumerable<SelectListItem> FounderInvestorList { get; set; }
    }

这是我的COntroller:

public ActionResult Create(PropertyViewModel _propertyViewModel)
    {
    if (ModelState.IsValid)
        {
        Property property = new Property();
        property.Id = _propertyViewModel.Id ?? 0;
        property.PropertyTitle = _propertyViewModel.PropertyTitle;
        ........other properties......        
        }
        _propertyQueryProcessor.Create(property);
        foreach(var investment in _propertyViewModel.FounderInvestments)
           {
               FounderInvestment _founderInvestment = new FounderInvestment
               {
                   Id = investment.Id??0,
                   InstallmentPeriod = investment.InstallmentPeriod,
                   InvestorId = investment.InvestorId,
                   PropertyId = investment.PropertyId,
                   Investment = investment.Investment
               };
               _founderInvestmentQueryProcessor.Create(_founderInvestment);    
    }

这是局部视图:

@model propertyMgmt.ViewModel.FounderInvestmentViewModel
@{
    ViewData.TemplateInfo.HtmlFieldPrefix = "PropertyViewModel"; //bind to main model
}

<div class="founderInvestmentDetails">
    @using (Html.BeginCollectionItem("founderInvestmentDetails"))
    {
        @Html.HiddenFor(m => m.Id, new { @class = "id" })

        @Html.HiddenFor(m=>m.PropertyId, new { @name = "PropertyId" })

        <div class="form-group">
            @Html.LabelFor(m => m.FounderInvestorList, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m=>m.FounderInvestorList,Model.FounderInvestorList , "Select Investor", htmlAttributes: new {@class = "form-control"})
                @Html.ValidationMessageFor(m => m.FounderInvestorList, "", new { @class = "text-danger" })
                @Html.HiddenFor(m => m.InvestorId, new { @name = "InvestorId" })
            </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>

最后,这是主要的观点:-

@model propertyMgmt.ViewModel.PropertyViewModel.PropertyViewModel
    @using (Html.BeginForm("Create", "Property", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <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" class="form-group">
        @foreach(var founderInvestmentDetails in Model.FounderInvestments)
        {
        @Html.Partial("_FounderInvestmentDetails", founderInvestmentDetails)
        }
    </div>

抱歉,这里有一个愚蠢的错误。正如@Stephen Muecke指出的那样

@using (Html.BeginCollectionItem("founderInvestmentDetails"))

应该

@using (Html.BeginCollectionItem("FounderInvestments"))

因为BeginCollectionItem使用Collection名称,所以它正在处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM