简体   繁体   中英

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

Only the value of the Partial view form is not being passed to the controller.:/ FounderInvestmentVM is the one whose partial view i have created,this VM is inside of PropertyVM.The other values are passed to the controller but not that of Partial View. It always gives FounderInvestments Count=0 when i put a debugger and see it :/

This is my PROPERTY VM containing FounderInvestorVM:-

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);
    }
}

This is 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; }
    }

This is My 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);    
    }

THIS IS THE PARTIAL VIEW:-

@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>

And finally this is the main VIEW:-

@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>

Sorry,there was a silly mistake here.As @Stephen Muecke pointed out

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

Should be

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

Because BeginCollectionItem uses name of the Collection it is working on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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