HTML - "Search by" and how to filter by selected field - asp.net-mvc

My task is to add a combo-box in the search page to allow a user to select the field she wants to search by and then search for customers using that field. In other words, there should be a “Search By” combo-box on the index page that the user selects the field to search by (Last Name, First Name, etc.) and then types in a search string and clicks a find button to perform a search and display the results on the same page. Search results should be sorted based on the field used for the search.
Currently, I have the following code:
<form asp-action="Index" method="get">
<div Class="form-actions no-color"></div>
<div class="search-item">Search</div>
<div class="sigform_fld search-item">
<select class="catalog">
<option value="1">Customers</option>
</select>
</div>
<div class="search-item">By</div>
<div class="sigform_fld search-item option">
<select class="option-1">
<option value="2">Last Name</option>
<option value="3">Email Address</option>
<option value="4">Company Name</option>
</select>
</div>
</form>
<form asp-action="Index" method="get">
<div Class="form-actions no-color">
<p>
<input type="text" name="LastName" value="#ViewData("currentFilter")" />
<input type="submit" value="Search" class="btn btn-default" />
| #Html.ActionLink("All Customers", "Index")
</p>
</div>
</form>
This only filters by Last Name. How can I have one search bar that filters by the selected option (Last Name, Company Name or Email address).
Help!

Related

Select2 and Thymeleaf integration

I created a simple search bar with multiple options enabled by the Select2 library:
<form th:action="#{/searchuser}" method="post">
<div th:object="${searchInput}">
<div class="input-group mb-3">
<input type="text" th:field="*{key}" class="form-control form-control-lg" placeholder="Search Here">
<div class="input-group-prepend">
<span class="input-group-text" type="submit"><i class="fas fa-search"></i></span>
</div>
</div>
<select class="select2-multiple-choices select2-choice" multiple="multiple" th:field="*{parameters}">
<option th:value="'id'" value="id"> Id</option>
<option th:value="'First Name'" value="First Name"> First
Name</option>
<option th:value="'Middle Name'"> Middle Name</option>
<option th:value="'Last Name'" value="Last Name"> Last Name</option>
<option th:value="'mail'" value="Mail"> E-Mail </option>
</select>
</div>
</div>
The controller:
#RequestMapping(value = "/searchuser", method = RequestMethod.POST)
public ModelAndView searchUser(Locale locale,
#ModelAttribute("searchInput") SearchUserDTO searchdto,
BindingResult searchBindResult,
Model model) {....}
SearchUserDTO contains String key and List parameters.
I would sent to server the search key and the selected options. How can I get the selection and pass them with thymeleaf?

How to temporary store in form data in .NET core

I am learning ASP.Net Core and I am creating a mock Flight booking app. On my home page, I have a search form to find a flight.
On submitting my form it goes to my SearchResultsController and then to my Search() action where I will run a search query.
What's the best way to pass the parameters through to this controller action for use in the Search() action method?
All the methods I've seen this far have involved creating new models but I don't want to save the searches to my database as it's not information that needs retaining. Having come to form a rails background I would use the parms hash which is a temporary data store.
Form:
<form asp-controller="SearchResults" asp-action="Search" method="post">
<div class="form-group">
<label for="fromAirport">Flying from:</label>
<select asp-for="AirportId" asp-items="#(new SelectList(ViewBag.ListOfAirports, "AirportId", "AirportCode"))" class="form-control">
</select>
<div class="form-group">
<label for="toAirport">Flying to:</label>
<select asp-for="AirportId" asp-items="#(new SelectList(ViewBag.ListOfAirports, "AirportId", "AirportCode"))" class="form-control">
</select>
</div>
<label for="fromAirport">Departure Date:</label>
<br />
<input type="date" />
<br />
<label for="fromAirport">No. passengers:</label>
<select class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<button type="submit" class="btn btn-primary mt-3">Search Flights</button>
</div>
</form>

Show message if there is an error in the form in angular 2

I have the following form and I need that after the user clicks Save, an error message shows next to the radio group that is required (but no radio selected). I tried the following but it doesn't show the message at all.
<form #formList="ngForm" (ngSubmit)="Save(formList)">
<div *ngFor="let item of data">
<p>{{item.name}}</p>
<input type="radio" name="{{item.id}}" [value]="1" [(ngModel)]="item.isSelected" required>Foo
<input type="radio" name="{{item.id}}" [value]="2" [(ngModel)]="item.isSelected">Bar
<div *ngIf="formList.controls.item?.id.required">
The radio is required!
</div>
</div>
<button type="submit">Save</button>
</form>
Try this code
<form #formList="ngForm" (ngSubmit)="Save(formList)">
<div *ngFor="let item of data">
<p>{{item.name}}</p>
<input type="radio" name="{{item.id}}" [value]="1" [(ngModel)]="item.isSelected" required>Foo
<input type="radio" name="{{item.id}}" [value]="2" [(ngModel)]="item.isSelected">Bar
<div *ngIf="!formList.form.valid">
The radio is required!
</div>
</div>
<button type="submit">Save</button>
</form>

Rails mechanize gem - scraping

I want to fill in some options which trigger the downloading of a file, using the Mechanize gem.
The HTML is the following.
<div>
<form action="" id="label_selectors" method="post">
...
...
<div class="source form_options">
<select name="source" id="source">
<option value="btce" selected="selected">Btc-e</option>
<option value="cryptsy">Cryptsy</option>
<option value="796">796</option>
<option value="bitstamp">Bitstamp</option>
<option value="formulas">Altcoin Indexes</option>
</select>
</div>
<div class="label form_options">
<select name="label" id="label">
</select>
</div>
<div class="period form_options">
<select name="period" id="period">
<option value="15m">15 minute</option>
<option value="1h" selected="selected">1 hour</option>
<option value="1d">1 day</option>
</select>
</div>
<div class="presense form_options">
CSV: <input type="radio" name="presence" value="csv"> Chart: <input type="radio" name="presence" value="chart" checked="checked">
</div>
<div class="submit form_options">
<input type="submit" name="submit" value="OK">
</div>
</form>
</div>
I tried to do something like this:
form = page.forms.first
form.source = "btce"
form.label = "BTC/USD"
form.period = "1d"
form.presense = "csv"
form.submit
However, it doesn't work:
NoMethodError: undefined method `presense=' for #<Mechanize::Form:0x007fb878dc7f98
(the typo presense rather than presence appears in the page)
How could I fill in this form correctly?
It looks like the presence input is a radio button.
To select a radio button with mechanize you'll want something like:
form.radiobuttons_with(name: 'presence')[0].check
There's other ways to select radio buttons with mechanize - you can see them here: http://docs.seattlerb.org/mechanize/GUIDE_rdoc.html

Rails select with multiple options only sends one

I have this form :
<form accept-charset="UTF-8" action="/contracts/<%= params[:id] %>/addConsultants" method="POST" id="updateConsultants"><!---->
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
<input name="authenticity_token" type="hidden" value="<%= session[:_csrf_token] %>">
</div>
<div class="consultant_selector">
<h2>Consultants</h2>
<select name="consultants_available" size="10" multiple>
<% #users.each do |u| %>
<option value="<%= u[:id_user] %>"><%= u[:name] %></option>
<% end %>
</select>
<ul>
<li class="triangle_right" id="add_consultant"></li>
<li class="triangle_left" id="remove_consultant"></li>
</ul>
<select name="consultants_selected" size="10" multiple>
</select>
</div>
<div>
<input type="submit" value="Mettre à jour" />
</div>
</form>
And in my controller :
def add_consultants
#contract = Contract.find(params[:id])
#consultants = params[:consultants_selected];
puts "------ Consultants: #{#consultants}"
redirect_to contract_path
end
The idea is that I can select names from the first select, move them to the second by clicking on the triangle and then submit the form to assign these names to a contract. However, when I do this the call to puts only prints one ID out of the 3 I have in the second select (all three of them have the selected attribute to true). How do I get multiple values?
Just found the answer here : https://stackoverflow.com/a/2196522/1796742
Changing the name of the second select from:
<select name="consultants_selected" size="10" multiple>
</select>
To :
<select name="consultants_selected[]" size="10" multiple>
</select>
Worked.

Resources