【问题标题】:How to pass partial view data to the parent view controller如何将部分视图数据传递给父视图控制器
【发布时间】:2019-08-14 03:56:59
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您需要使用模板化助手

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

    Check This

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2014-08-14
      • 2020-04-01
      相关资源
      最近更新 更多