如何在使用局部视图时传递父视图数据

时间:2015-10-22 12:42:22

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

我的父视图中有一个下拉列表,当我选择一个值时,我想保存该值,并在单击部分视图中单击按钮时使用它。目前,如果我单击部分视图中的按钮,它将不具有父视图的值。但是,如果我将下拉列表移动到局部视图,一切正常,但我想将其保留在父视图中。

父视图(这是下拉列表所在的位置)

../img/bg.png

部分视图

/resource/img/bg.png

父模型

<div class="btn-toolbar">
    <div class="btn-group">
        <button type="button" class="btn btn-primary" id="FetchProcessId">Fetch Process Id</button>
    </div>
</div>

<div class="col-md-3" style="@displayAdminLead">
    <div class="dropdown div-inline">
        @{
            var envlist = new List<SelectListItem>()
            {
                new SelectListItem() {Value = "OAK", Text = "OAK"},
                new SelectListItem() {Value = "QA", Text = "QA"},
                new SelectListItem() {Value = "PROD", Text = "PROD"}


            };
        }
        @Html.DropDownListFor(m => m.EnvironmentName, envlist, "Environment", new {@class = "form-control"})
    </div>
</div>


<div style="overflow: auto">
        @using (Html.BeginForm())
        {
            <div id="FetchProcessIdDiv" style="">
                @Html.Action("FetchProcessId", Model)
            </div>
        }
</div>

控制器

<table class="table table-hover">
    <tbody>
        <tr>
            <td>System Name:</td>
            <td>
                @Html.DropDownListFor(m => m.FetchProcessIdModel.SystemName, new SelectList(Model.FetchProcessIdModel.DropDownOptions, "valueId", "value"))
            </td>
            <td rowspan="2">@Html.TextAreaFor(m => m.FetchProcessIdModel.Message, 12, 50, null)</td>
        </tr>
        <tr>
            <td>Deliverable Id:</td>
            <td>@Html.TextBoxFor(m => m.FetchProcessIdModel.DeliverableId)</td>
        </tr>
    </tbody>
</table>

<input type="submit" name="submit" class="btn btn-default" value="Fetch Process Id" />

1 个答案:

答案 0 :(得分:1)

为了让输入将它的值发布到控制器,它需要在表单元素中。

    <div style="overflow: auto">
        @using (Html.BeginForm())
        {
            <div class="btn-toolbar">
                <div class="btn-group">
                    <button type="button" class="btn btn-primary" id="FetchProcessId">Fetch Process Id</button>
                </div>
            </div>

            <div class="col-md-3" style="@displayAdminLead">
                <div class="dropdown div-inline">
                    @{
                        var envlist = new List<SelectListItem>()
                        {
                            new SelectListItem() {Value = "OAK", Text = "OAK"},
                            new SelectListItem() {Value = "QA", Text = "QA"},
                            new SelectListItem() {Value = "PROD", Text = "PROD"}
                        };
                    }

                    @Html.DropDownListFor(m => m.EnvironmentName, envlist, "Environment", new {@class = "form-control"})
                </div>
            </div>


            <div id="FetchProcessIdDiv" style="">
                @Html.Action("FetchProcessId", Model)
            </div>
        }
    </div>