简体   繁体   中英

How to pass partial view data to the parent view controller

I am new to 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. 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 :

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

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

Check This

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