如何将动态数据作为模型从视图传递到控制器

时间:2017-12-11 13:54:59

标签: c# ajax asp.net-mvc asp.net-mvc-ajax

我正在使用Ajax MVC将模型提交给控制器,In View我正在显示来自API的数据,如下面的代码:

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

我的要求是我们检查的任何内容&#39; isCapabilityChecked&#39;检查我需要保存到数据库中的项目。我正试图通过“儿童元素”#39;数据作为模型从视图到控制器而不使用jquery。我的要求要求使用模型提交本身。有没有可能的方法使用模型提交将此子元素列表提交给控制器?请帮助我。

//更新:

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

0 个答案:

没有答案