Hi,
How to redirect page to specific tabs in mvc
I have a page that has 2 tabs which is #tabs-1 and #tabs-2.
I want to redirect the page to #tabs-2 after submit form. I tried the code but it didn't work.
Here is my code.
FormRequest View
<script type="text/javascript">
$(function () {
if ($('#hfSelected').val() != '') {
var enabled = [];
var disabled = [];
if ($('#hfEnabled').val() != '') {
enabled = $('#hfEnabled').val().split(',').map(Number);
}
if ($('#hfDisabled').val() != '') {
disabled = $('#hfDisabled').val().split(',').map(Number);
}
$("#tabs").tabs({ active: $('#hfSelected').val(), enabled: enabled, disabled: disabled });
} else {
$("#tabs").tabs({ disabled: [1, 2] });
}
$('#ddlMaterial').change(function () {
$('#req_material').val($(this).find('option:selected').text());
});
$('#ddlQuantity').change(function () {
$('#req_quantity').val($(this).find('option:selected').text());
});
});
$(function () {
var selector = '@ViewBag.ActiveTab';
if(selector)
{
$("#tabs"+selector).tab('show');
}
});
</script>
<input type="hidden" value="@TempData["Selected"]" id="hfSelected" />
<input type="hidden" value="@TempData["Enabled"]" id="hfEnabled" />
<input type="hidden" value="@TempData["Disabled"]" id="hfDisabled" />
<div id="tabs">
<ul class="nav nav-pills">
<li class="active"><a href="#tabs-1" data-toggle="tab">Details</a></li>
<li><a href="#tabs-2" data-toggle="tab">Material</a></li>
</ul>
<div class="tab-pane active" id="tabs-1"> Hello
<input id="btnSaveMaterial" type="submit" value="Next" class="btn btn-primary" />
</div>
<div class="tab-pane" id="tabs-2">
@using (Html.BeginForm("AddMaterial", "Home", FormMethod.Post))
{
//////form section details
</div>
}
AddMaterial controller
[HttpPost]
public ActionResult AddMaterial(ItemModel model, string id)
{
TempData["Selected"] = "2";
TempData["Enabled"] = "0,1,2";
id = Convert.ToString(TempData["NewID"]);
ViewBag.ActiveTab = id;
////insert data into database part
return new RedirectResult(Url.Action("FormRequest", new { id }) + "#tabs-2");
}