Making Request to Controllers - ASP.NET Core - javascript

I have a form to fetch data from my database based on the asp-action. The form looks like below
HTML
<form id="user__collection" asp-action="Admins" asp-controller="User" method="get">
<input name="collection"
type="text"
id="user_search"
/>
<button type="submit">
</button>
</form>
Now i have two menu tabs named Admins and Users . When i am in the admin tab and i search for a surname, it fetches from the database and display both Admins and Users under the respective tabs, that is the logic and works as expected
Now when i am in users tab and i search for a surname, after the page reloads, it goes back to the Admin tab, and i have to select the Users tab to see the results.
My issue here is, is there a way i can make sure after the page reloads, it stays in the users tab without having to go to the Admin tab first since that is where i made the request from.
My idea was to find a way to use JS to set asp-action based on the URL.
Admin Menu url - localhost:8000/admins
Users Menu url - localhost:8000/users
UserController
public async Task<IActionResult> Users([BindRequired] string collection)
{
//fetch users from database and display
}
public async Task<IActionResult> Admins([BindRequired] string collection)
{
//fetch admins from database and display
}

U can set a Referrer link to take U back to the previous page .
Proceed as below :
ViewBag.Url = System.Web.HttpContext.Current.Request.UrlReferrer;
And then in the controller use this :
return Redirect(ViewBag.Url);
And if U are using html tabs ,the solution may not be working because html tabs don't have urls.
To solve this U have to Create partial views and now U have their urls.
Good Luck

Maybe you can use at controller:
return RedirectToAction("Users");

Related

How do I get a separator with a variable inside the URL in Laravel 5.8

I'm using Laravel 5.8 to develop a project. I have a page which contains a button, clicking on it will redirect you to a registration page. This button is located in multiple pages, so, when the button is clicked, I want the name of the page from which it was clicked to be shown in the URL.
For Example, take this Youtube link, https://www.youtube.com/watch?v=dQw4w9WgXcQ
The /watch?v=dQ part, I want to have that in my URL, where "watch" would be the current page's name, and v would contain the name/id of the previous page. I have posted the button and the routes below.
Let me know if something else is required.
Request a Demo
Route::get('demo','Controller#dropdown_data');
Route::POST('demo','Controller#submit');
The get route calls a function to retrieve data for dropdown fields from a database.
The post route calls the function for submission of the details.
With the url() helper it is not possible to add URL query parameters. However, when you add a name to the route, you can use the route() helper like this on your pages:
<a href="{{ url ('demo', ['prev' => 'nameOfCurrentPage')}}" class="set-btn btn3">
Request a Demo
</a>
And like this as your route definition:
Route::get('demo','Controller#dropdown_data')->name('demo');

How to update or refresh table using Rails Actioncable?

My action cable is working fine, it shows me an alert with the data content(although it shows in all the pages).
This is my case: an user with role ADMIN has access to an url (http://localhost:3000/ventas/) with a table (html id=tablaventas)that show a list of sales. This table must be automatically refreshed every time a user (role SELLER) inserts a new sale in the database.
This user with role seller has an url (http://localhost:3000/ventas/new) with a form where insert new sales to the database. The Actioncable implementation is working okay, but I have two problems:
1- how to refresh the table from the ventas.coffee:
//here i am stuck
received: (data) ->
alert data.name //this works ok
//...AND HERE I WANT TO UPDATE or refresh THE ADMIN TABLE
2- Also, how can I show for example the alert message only to the admin specific view? in this case only in the view ventas/index.html.erb.
Can you just add another row to the table?
$('#tablaventas').append("<tr><td>#{data.name}</td>...other tds...</tr>")
If you need to refresh the whole table - you'll need to call .html(data.table_html) on table container element. Also this means that you'll have to render table_html in your controller, via render_to_string('table') or something.
I think your issue is that you subscribe to action cable updates globally while you only need to subscribe on the index page. This blog post should help you solve this.
Hope the above helps.

thymeleaf :: populate the form with data from the service

I have a controller as below
#RequestMapping(value = "/user/{userId}/personal", method = RequestMethod.GET)
public String getUserPersonalInfo(#PathVariable("userId") Integer userId, Model model) throws ServiceBusinessException {
UserBO userBO = userServiceImpl.findUserByUserId(userId);
model.addAttribute("user", userBO);
model.addAttribute("userId", userId);
model.addAttribute("genders", EnumSet.allOf(Gender.class));
model.addAttribute("maritalStatuses", EnumSet.allOf(MaritalStatus.class));
model.addAttribute("ethnicities", EnumSet.allOf(Ethnicity.class));
System.out.println(EnumSet.allOf(Ethnicity.class));
return "personal";
}
Please note the following :
this is a GET call made from the UI to the controller, the controller then processes the request and returns the user along with the other information to the view (personal.html).
Now on the personal.html i need to have a way to edit this user information. thus I am thinking of binding this user data in a th:form tag
this form would then be used to send a post request to the controller to save the changes made by the user.
The problem is that I have not seen any code examples where this has been done. All examples which I have seen, the form is empty and the page is then loaded. user fills up the empty form and click on submit, this information is then stored in the db.
I however on the other hand, is doing something opposite. I need to populate the form with the user object on page load and then on submit this form should be saved in the database.
Can someone guide me to this example please ?
#user641887
While populating the user details on page load, bind data using th:field for a read-only form and when user clicks on edit button make the form fields which are allowed to edit as editable. Now user can edit the required information and save the details.
Sample thymeleaf form code
<input type="text" th:field="*{datePlanted}" />

Pass values to action=URL in a HTML form and get the response

I've an e-store, one of my payment methods is (Mobile Recharge Cards)
Recently I was doing every thing myself, i receive the recharge number and recharge it on my phone, then mark the order as 'complete' if the number is correct and if not i just ignore it
Now it's too hard to do it, so I searched the web for some solution and i find a one
This webpage:
http://www.stc.com.sa/wps/wcm/connect/english/individual/mobile/prepaid/Charging-Sawa
has a form that send the card balance directly into my phone, and it shows whither the card number is correct or not
So, I'm trying to create an api from this form to recharge the card directly into my phone
I checked this link and i found that it's call another webpage (https://my.stc.com.sa/wps/portal/sawaRecharge/en?cardtype=sawacard&customertype=individual) and include it inside an iframe
I don't want anyone to create the API, all I need is someone who can help me to takeout the (Form Action URL), their system is keep changing the (action="https://my.stc.com.sa/wps/portal/!ut/p/b1/.........") URL each time I refresh the webpage, also they don't even include the Action URL direct in the form tag like this:
<form name="sawaRechargeContestForm" method="post"
action"/wps/portal/!ut/p/b1/......../">
..............................
</form>
BUT, they coded it like this:
First they create an A tag and then include the Action URL of the form, which keep changing every time the page is refreshed:
<a id="wpf_action_ref_3UISawaRechargeSawaPublicUI15d621bb54d174580_1"
href="/wps/portal/!ut/p/b1/......../" style="display:none"></a>
Then they call the form and define it with (_f) variable
_f = document.forms['rechargePublicSawaPageForm'];
Then they add the Action URL from the previous (A tag)
_f.action = document.getElementById('wpf_action_ref_2UISawaRechargeSawaPublicUI15d621bb54d174580_1').href;

Get elements of a query-set with a jquery get function. Django

In my django app I would like to display a text when a user clicks on a button, without refreshing the page. When the user clicks again, I would like an other text to be displayed, still without refreshing the page.
The informations (texts) I wan't to display are in a query set, named "information".
So I would like to know how to accomplish that.
Here is the way I try to do it:
I create a view where I store my query set:
def get_information (request):
information= Information.objects.filter(object1__id= X)
Then I create a jquery get function (in the template of the page where the user clicks) in order to get informations on this list:
function get_info(){
$.get('/mysite/get_information', {'information':information}, function(data) {
$('.information').html(data);
});
};
And then I render it in my template with a submit button and a onclick="get_info".
But I don't know how to make the request get a different information at each request, in order that the user does not get the same information twice.
Thank you a lot for your help.
Take a look in to Django Pagination. You could create the Paginator object in your view and then you can display message from the queryset based on the request from the client side. Let me know in case of any issues.

Categories

Resources