[英]How to pass parent view data when using partial view
I have a dropdownlist in my parent view and when I select a value I want to save that value and use it when I click a button in my partial view. 我的父视图中有一个下拉列表,当我选择一个值时,我想保存该值并在局部视图中单击一个按钮时使用它。 Currently if I click a button in the partial view it will not have the value of the parent view.
当前,如果我在局部视图中单击一个按钮,它将没有父视图的值。 However, if I move the dropdownlist to the partial view everything works fine, but I would like to keep it in my parent view.
但是,如果将下拉列表移至部分视图,则一切正常,但我希望将其保留在父视图中。
Parent View (This is where the dropdownlist is) 父视图(这是下拉列表所在的位置)
<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>
Partial View 部分视图
<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" />
Parent Model 父模型
public class ParentModel
{
public string EnvironmentName { get; set; }
}
Controller 控制者
[HttpPost]
public ActionResult ParentClass(string submit, ParentModel model)
{
//omit
var envName = model.EnvironmentName;
//I am not seeing a value for model.EnvironmentName at this point
}
In order for an input to post it's value to the controller, it needs to be inside a form element. 为了使输入将其值发布到控制器,它必须位于form元素内。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.