1

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
    }
1
  • As long as the dropdown is in the same form as the submit button, it should work. Post your parent view code, including the form and where you load the partial. Commented Oct 22, 2015 at 13:14

1 Answer 1

2

In order for an input to post it's value to the controller, it needs to be inside a form element.

    <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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.