If your primary goal is to handle retaining the appropriate tab after a submission (which is is going to handle resetting your page since you aren't doing it via AJAX). You can consider submitting the current tab whenever you submit.
This might involve storing the ID within a hidden element in your form:
<!-- Place this within your form -->
<input type='hidden' name='selectedTab' value='0' />
And then when your tabs are changed, simply update this value:
// Whenever a tab is clicked
$('#tabs li > a').click(function(){// Omitted for brevity// Update the current state of the tab
$("[name='selectedTab']").val(data_id);});
If you take this approach, you'll likely want to add a property for this state within your ViewModel:
publicclassYourViewModel{// Omitted for brevitypublicintSelectedTab{get;set;}}
And then within your document ready block, you could check the value of this to determine which table to select:
// Select the tab for a given index
$('#tabs li a').eq(@Model.SelectedTab).tab('show');
Hi Rion, Thanks a lot for your quick response, I will try it tomorrow when I back to office and let you know then.
But before trying your codes, I just want to let you know if I can use Cookie to do the same thing, since it is only client side data, not necessary to pass the tab ID to server.
Basically, when I click the second tab, I use the similar codes as yours to save the current tab ID to a cookie ("currentTabValue") , and after the data is saved successfully, the Edit razor view will be loaded, and at the JavaScript loading event of Edit
razor view, the JavaScript codes to retrieve the cookie value to see if it is second tab (the currentTabValue == "2"), if it is "2", then I will use JavaScript to make second tab click(). after the second is clicked, then set that cookie expired,
This method I need to maintain the cookie value to make sure it is correct and delete it properly, otherwise, it will create confusing
What do you think? if your solution is simple, then I will use yours,
Member
366 Points
2214 Posts
Re: How to passing data from razor view to controller in this case?
Jan 08, 2020 08:37 PM|Peter Cong|LINK
Hi Rion, Thanks a lot for your quick response, I will try it tomorrow when I back to office and let you know then.
But before trying your codes, I just want to let you know if I can use Cookie to do the same thing, since it is only client side data, not necessary to pass the tab ID to server.
Basically, when I click the second tab, I use the similar codes as yours to save the current tab ID to a cookie ("currentTabValue") , and after the data is saved successfully, the Edit razor view will be loaded, and at the JavaScript loading event of Edit razor view, the JavaScript codes to retrieve the cookie value to see if it is second tab (the currentTabValue == "2"), if it is "2", then I will use JavaScript to make second tab click(). after the second is clicked, then set that cookie expired,
This method I need to maintain the cookie value to make sure it is correct and delete it properly, otherwise, it will create confusing
What do you think? if your solution is simple, then I will use yours,
thanks