Reputation: 3
I have a main view with a textbox and dropdown list:
@using (Ajax.BeginForm("GetResults", "SomeController", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "PartialDiv" }))
<select name ="dropdown1" onchange="$(this.form).submit();" >
<option></option>
@foreach (var recordrow in ...)
{
<option value="@recordrow.Value" >@recordrow.Text</option>
}
</select>
<select name ="dropdown2" >
<option></option>
@foreach (var recordrow in ...)
{
<option value="@recordrow.Value" >@recordrow.Text</option>
}
</select>
@Html.TextBox("textbox1", null, new { size = 10 })
<div id="PartialDiv" class="GroupBoxForeground" >
@Html.Partial("PartialView")
</div>
The main view automatically renders a partial view once certain controls on the main view have been filled with values. Inside the Partial View, there is a submit button which calls an ActionResult.
Part of the Partial View:
@using (Ajax.BeginForm("Function1", "SomeController", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "someDiv" }))
...
<input type="submit" name="btn_submit" value="Process" style="width:80px" />
My ActionResult looks like this:
[HttpPost]
public ActionResult Function1(FormCollection formdata, MasterModel MstModel)
{
...
}
I am able to successfully bind the values in my Partial View to MasterModel. However, I am unable to get the values of the controls in the Main View while calling the ActionResult. I cannot find the key in the FormCollection and have tried the same method I used in the partial view for model binding.
Both Views are strongly-typed with the MasterModel.
Since the values in the Main View can be changed after the Partial View is rendered, I need to get the values once more when the submit button is clicked in the partial view. Is there a way to access those control values in the Main View inside ActionResult of the Partial View?
Edit: To explain the situation more clearly as suggested in the comments, My main view is basically a search screen with a textbox and one of the dropdown lists which will be used by the partial view for processing later.
My partial view (in a separate form) shows the results of the search along with a submit button inside to process certain tasks based on the results. However, this processing also requires the 2nd dropdown list's value and the textbox's value in the main view. Those 2 controls in the main view can be changed even after the partial view is loaded. Therefore, I am hoping to find some way to get those values in the main view when the submit button is pressed.
Upvotes: 0
Views: 1010
Reputation: 3024
so you mean, you have two separate forms. On posting second form, you also want to post values of the controls, that are in first form. For that purpose, you can take help of jquery
, as values outside the form are not posted on submit
.
You can create hidden
fields in your second form (partial view form), as:
@Html.Hidden("dropdown1value", "", new { @id = "ddl1val" })
@Html.Hidden("dropdown2value", "", new { @id = "ddl2val" })
and then on change event of your dropdown1, you can do like:
//assuming 'dropdown1' is the `id` of your dropdown1
$("#dropdown1").change(function () {
$("#ddl1val").val($("#dropdown1").val());
});
same way for the second dropdown. Hope you have got the idea.
Upvotes: 0