如何将部分视图数据传递到父视图控制器

时间:2019-08-14 03:56:59

标签: c# asp.net-mvc

我是ASP.NET MVC的新手。我有一个父视图和一个局部视图,都使用不同的模型。我担心的是,当我提交页面时,部分视图数据也应该传递给父视图HTTP Post方法。我已经在父视图模型中创建了一个属性,以从部分视图模型中获取数据。但是,当我提交页面时,我得到的是空值。任何帮助将不胜感激

父视图caseDetails.cshtml

@model EMSD.Module.Case.CPN.Model.CPNDetailViewModel
@{
    ViewBag.Title = "_CPNCaseDetail";
}
<table class="table table-striped">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <tr>
            <td class="leftheaderform">
                @Html.LabelFor(model => model.CPN_CAT)
                <span style="color:red">*</span>
            </td>
            <td class="rightdetailform" colspan="3">
                @Html.DropDownListFor(model => model.CPN_CAT, new SelectList(Model.InformedCat, "ID", "Name"), "--Select--", new { @class = "form-control form-control-sm col-3" })
                @Html.ValidationMessageFor(model => model.CPN_CAT, "", new { @class = "text-danger" })
            </td>
        </tr>

        <tr>
            <td class="leftheaderform">
                @Html.LabelFor(model => model.CPN_CAT_RMK)
            </td>
            <td class="rightdetailform" colspan="3">
                @Html.TextAreaFor(model => model.CPN_CAT_RMK, new { htmlAttributes = new { @class = "form-control form-control-sm" }, rows = 2, style = "width: 100%; max-width: 100%;" })
                @Html.ValidationMessageFor(model => model.CPN_CAT_RMK, "", new { @class = "text-danger" })
            </td>
        </tr>

*used HTML.partial for calling partial view*

  @Html.Partial("~/Views/Shared/Address.cshtml", Model.Address)
</table>

父视图模型:

public class CPNDetailViewModel
{
    [DisplayName("Informed Category")]
    public string CPN_CAT { get; set; }

    [DisplayName("Remarks ")]
    public string CPN_CAT_RMK { get; set; }

    // property for getting data from partial view
    public UpdateGasSupplierViewModel Address { get; set; }
}

局部视图Address.chtml

@model EMSD.Module.Misc.Model.UpdateGasSupplierViewModel
<table class="table table-striped">
    <tr>
        <td><font color="blue">Search Address</font></td>
        <td colspan="4"> <input id="FreeEnglishAddressText" class="form-control" /></td>
        <td><button type="button" onclick="callAPI()" class="btn btn-outline-primary form-control">Search</button></td>

    </tr>
    <tr>
        <td>
            Flat
        </td>
        <td>

            @Html.DropDownListFor(model => model.GSC_ENG_FT, new SelectList(Model.FlatList, "ID", "Name"), "--Select--", new { @class = "form-control" })
        </td>
        <td>
            @Html.EditorFor(model => model.GSC_ENG_FT_2, new { htmlAttributes = new { @class = "form-control" } })
        </td>
</tr>
</table>

局部视图模型:

namespace EMSD.Module.Misc.Model
{
    public class UpdateGasSupplierViewModel
    {
        public string GSC_ID { get; set; }

        public string GSC_COY_ENAME { get; set; }
    }
}

父控制器方法:

[HttpPost]
public ActionResult _CPNCaseDetail(CPNDetailViewModel model)
{
        string Post = Session["user_post"].ToString();

        if (ModelState.IsValid)
        {
            cPNCaseService.Save(model);
        }

        return RedirectToAction("Case", "Case", new { Id = model.CASE_ID, Id2 = queueId, Id3 = "", Id4 = "Y" });
}

1 个答案:

答案 0 :(得分:1)

您需要使用模板化助手

带模板的帮助器与部分帮助器的不同之处在于,只要我们使用Html.EditorXyz()HtmlHelper方法,父级的特殊上下文信息就会传递给子级。

Check This