How to pass dynamic data as model from view to controller

问题内容:

I am using Ajax MVC to submit the model to controller, In View I am displaying the data from a API like below code:

 @using (Ajax.BeginForm("SaveFeaturesCapabilities", "Estimates", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "step-3" })) {

@for (int i = 0; i < Model.Capabilitiesitems.Count(); i++)
        {
           // I can submit this loop iteration whatever 'isCapabilityChecked' item checked data  as model to the controller by using hidden fields
            string PercentDoneByStoryPlanEstimate = Model.Capabilitiesitems[i].PercentDoneByStoryPlanEstimate + "%";
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].CapabilityID)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].CapabilityName)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].ACProjectName)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].PreliminaryEstimate)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].RefinedEstimate)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].LeafStoryCount)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].LeafStoryPlanEstimateTotal)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].PercentDoneByStoryPlanEstimate)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].PlannedStartDate)
            @Html.HiddenFor(m => Model.Capabilitiesitems[i].PlannedEndDate)
        <tr data-tt-id="@Model.Capabilitiesitems[i].CapabilityID.ToString().Trim().ToUpper()" class="bd-btm-gray">
            <td class="pos-identer">
                @*<input type="checkbox" class="pos-input2" />*@ 
                @Html.CheckBoxFor(m=>Model.Capabilitiesitems[i].isCapabilityChecked,new {@class= "pos-input2",@style= "width:11px;" })
            </td>
            <td style="width: 150px; display: inline-block" class="bd-none"> <strong class="pos-cap">@Model.Capabilitiesitems[i].CapabilityID</strong></td>                              
            <td style="width: 150px;"><strong  class="prog-proj-crop">@Model.Capabilitiesitems[i].CapabilityName</strong></td>
            <td style="width: 150px;" ><strong class="prog-proj-crop">@Model.Capabilitiesitems[i].ACProjectName</strong></td>
            <td>@Model.Capabilitiesitems[i].PreliminaryEstimate</td>
            <td>@Model.Capabilitiesitems[i].RefinedEstimate</td>
            <td class="text-right">@Model.Capabilitiesitems[i].LeafStoryCount</td>
            <td class="text-right">@Model.Capabilitiesitems[i].LeafStoryPlanEstimateTotal</td>
            <td class="text-right">@PercentDoneByStoryPlanEstimate</td>
            <td class="text-right">@Model.Capabilitiesitems[i].PlannedStartDate</td>
            <td class="text-right">@Model.Capabilitiesitems[i].PlannedEndDate</td>
            <td class="text-right editable-col"><span class="editable-span">&nbsp;</span></td>
            <td class="text-right">&nbsp;</td>
            <td class="text-right">&nbsp;</td>
            <td class="text-right">&nbsp;</td>
            <td class="text-right">&nbsp;</td>
            <td class="text-right">&nbsp;</td>
        </tr>
            var childElements = Model.FeaturesItems.Where(m => m.CapabilityID == Model.Capabilitiesitems[i].CapabilityID).ToList();

            foreach (var feature in childElements)
            {

              //I am unable to submit this data as it is not in the model.
                string fPercentDoneByStoryPlanEstimate = feature.PercentDoneByStoryPlanEstimate + "%";
            <tr data-tt-id="@feature.FeatureID" data-tt-parent-id="@feature.CapabilityID.ToString().Trim().ToUpper()">
                <td>@Html.CheckBoxFor(m=>Model.FeaturesItems[i].isCapabilityChecked,new {@class= "pos-input2",@style= "width:11px;" }) </td>                   
                <td><a href="#" class="link-button pos-left-3">@feature.FeatureID</a></td>
                <td class="prog-proj-crop">@feature.FeatureName</td>
                <td class="prog-proj-crop">@feature.ACProjectName</td>
                <td>@feature.PreliminaryEstimate</td>
                <td>@feature.RefinedEstimate</td>
                <td class="text-right">@feature.LeafStoryCount</td>
                <td class="text-right">@feature.LeafStoryPlanEstimateTotal</td>
                <td class="text-right">@fPercentDoneByStoryPlanEstimate</td>
                <td class="text-right">@feature.PlannedStartDate</td>
                <td class="text-right">@feature.PlannedEndDate</td>
                <td class="text-right editable-col"><span class="editable-span">&nbsp;</span></td>
                <td class="text-right">&nbsp;</td>
                <td class="text-right">&nbsp;</td>
                <td class="text-right">&nbsp;</td>
                <td class="text-right">&nbsp;</td>
                <td class="text-right">&nbsp;</td>
            </tr>
           }
       }

My requirement is whatever we checked ‘isCapabilityChecked’ checked that items I need to save into database. I am trying to pass ‘childElements’ data as model from view to controller without using jquery. My requirement demands to use model submit itself. Is there any possible ways to submit this child elements list to controller using model submit? please help me anyone.

//Updated:

   [HttpPost]
    public ActionResult SaveFeaturesCapabilities(PortfolioItems model)
    {
        var list = model.Capabilitiesitems.Where(m => m.isCapabilityChecked).ToList();
       var featureslist = model.FeatureItems.Where(m => m.isCapabilityChecked).ToList();

          return Json("", JsonRequestBehavior.AllowGet);
    }

问题评论:

    
Please, post your MVC Action method.
    
please see my updated post
– Dot Net Developer
57 mins ago
    
Parameters are mapped using the name input property. Add a hidden field in the view and a new parameter either in model or in the Action.

原文地址:

https://stackoverflow.com/questions/47754484/how-to-pass-dynamic-data-as-model-from-view-to-controller

添加评论

友情链接:蝴蝶教程