简体   繁体   English

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

[英]How to pass partial view data to the parent view controller

I am new to ASP.NET MVC. 我是ASP.NET MVC的新手。 I have a parent view and a partial view, both using different models. 我有一个父视图和一个局部视图,都使用不同的模型。 My concern is when I submit the page, the partial view data also should pass to the parent view HTTP Post method. 我担心的是,当我提交页面时,部分视图数据也应该传递给父视图HTTP Post方法。 I had created a property in the parent view model to get the data from the partial view model. 我已经在父视图模型中创建了一个属性,以从部分视图模型中获取数据。 But when I submit the page, I am getting null. 但是,当我提交页面时,我得到的是空值。 any help would be appreciated 任何帮助,将不胜感激

Parent view caseDetails.cshtml : 父视图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>

Parent view model: 父视图模型:

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

Partial view Address.chtml : 部分视图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>

Partial view model: 局部视图模型:

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

        public string GSC_COY_ENAME { get; set; }
    }
}

Parent controller method: 父控制器方法:

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

You need to use Templated helpers 您需要使用模板化助手

Templated helpers are different than partials in that special contextual information from the parent is passed down to the child as long as we're using the Html.EditorXyz() HtmlHelper methods. 模板化的帮助器与部分帮助器不同,只要我们使用Html.EditorXyz()HtmlHelper方法,来自父级的特殊上下文信息就会传递给子级。

Check This 检查这个

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

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