Force single select with Razor Select Tag Helper - asp.net-core

I'm Struggeling to find a solution to the following Problem:
My Razor-Page has a form with two select-elements, only one of which will be active (not disabled) at a time. Their values are associated to the same Property in my model.
I had to make my Property an Array, so I could access the value of both selects, despite one of them always being "null".
This has lead to my select being rendered with the "multiple" attribute in HTML. How can I get rid of this? I want it to be single select.
My selects:
<select asp-for=TätigkeitIds required onchange="this.form.submit()" disabled=#(Model.Erfassung.TätigkeitNavigation != null && Boolean.Equals(Model.Erfassung.TätigkeitNavigation.Abzurechnen, true))>
#{
<option selected=#(Model.Erfassung.TätigkeitNavigation == null) value="">Bitte eine nicht abzurechnende Tätigkeit wählen</option>
foreach(StammdatenTätigkeit Tätigkeit in await Model.GetTätigkeitenAsync(false))
{
<option selected="#(Model.Erfassung.TätigkeitNavigation != null && String.Equals(Tätigkeit.Id, Model.Erfassung.TätigkeitNavigation.Id))" value=#Tätigkeit.Id>#Tätigkeit.Name</option>
}
}
</select>
<select asp-for=TätigkeitIds required onchange="this.form.submit()" disabled=#(Model.Erfassung.TätigkeitNavigation != null && Boolean.Equals(Model.Erfassung.TätigkeitNavigation.Abzurechnen, false))>
#{
<option selected=#(Model.Erfassung.TätigkeitNavigation == null || Boolean.Equals(Model.Erfassung.TätigkeitNavigation.Abzurechnen, false)) value="">nicht abrech. Std.</option>
foreach(StammdatenTätigkeit Tätigkeit in await Model.GetTätigkeitenAsync(true))
{
<option selected="#(Model.Erfassung.TätigkeitNavigation != null && String.Equals(Tätigkeit.Id, Model.Erfassung.TätigkeitNavigation.Id))" value=#Tätigkeit.Id>#Tätigkeit.Name</option>
}
}
</select>
The Property:
[BindProperty(SupportsGet = false)]
public String[] TätigkeitIds { get; set; }
The rendered HTML:
<select required onchange="this.form.submit()" id="T_tigkeitIds" multiple="multiple" name="TätigkeitIds">
<option value="" selected="selected">Bitte eine nicht abzurechnende Tätigkeit wählen</option>
<option selected="selected" value="d58559ac-6601-4871-aff2-aa72982fd5cc">Schulung</option>
</select>
<select required onchange="this.form.submit()" disabled="disabled" id="T_tigkeitIds" multiple="multiple" name="TätigkeitIds">
<option selected="selected" value="">nicht abrech. Std.</option>
<option value="c2b8fd29-c85b-49c1-a2db-35b9fa9d11a0">PJ-Bearbeitung</option>
</select>
Essentially, I am looking for a way to either force the HTML-Select to appear without the multiple-attribute or to make my Property a normal String again and tell the framework to fill it with whichever input isn't null.

Based on the comment from Mike Brind, I was able to solve my problem by simply deactivating the tag helpers for my two select-Tags like so:
<!select name="TätigkeitIds" required onchange="this.form.submit()" #(Model.Erfassung.TätigkeitNavigation != null && Boolean.Equals(Model.Erfassung.TätigkeitNavigation.Abzurechnen, false) ? "disabled" : "")>
#{
<option selected=#(Model.Erfassung.TätigkeitNavigation == null || Boolean.Equals(Model.Erfassung.TätigkeitNavigation.Abzurechnen, false)) value="">nicht abrech. Std.</option>
foreach(StammdatenTätigkeit Tätigkeit in await Model.GetTätigkeitenAsync(true))
{
<option selected="#(Model.Erfassung.TätigkeitNavigation != null && String.Equals(Tätigkeit.Id, Model.Erfassung.TätigkeitNavigation.Id))" value=#Tätigkeit.Id>#Tätigkeit.Name</option>
}
}
</!select>

Related

Select Option (for dropdown) Laravel

I make a dropdown for a form, I will show the code below. However, when I click the submit button, there is an error saying,
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'brand' cannot be null (SQL: insert into supplier_details.
The data that I chose from the dropdown is actually null. Actually, I'm new to Laravel.
I don't want to make a dropdown list from a database, I just want to display the option and the option will be inserted into the database when the user clicks the submit button after filling in the form.
<div class="form-group row">
<label style="font-size: 16px;" for="id" class = "col-sm-2">Item Brand </label>
<label for="supp_name" class = "col-sm-1">:</label>
<div class="col-sm-7">
<select name="brand" class="form-control js-example-basic-single" required>
<option >Please select item brand</option>
<option value="machine1"> Item Brand 1 </option>
<option value="machine1"> Item Brand 2 </option>
<option value="machine1"> Tem Brand 3 </option>
</select>
</div>
</div>
Controller
public function createsupplierdetails()
{
return view ('frontend.praiBarcode.getweight');
}
public function supplierdetails(Request $r)
{
$details = new SupplierDetail;
$getuserPO = Supplier::where('PO',$r->PO)->first();
$details->brand = $getuserPO->brand;
$details->container_no = $getuserPO->container_no;
$details->date_received = $getuserPO->date_received;
$details->gross_weight = $getuserPO->gross_weight;
$details->tare_weight = $getuserPO->tare_weight;
$details->net_weight = $getuserPO->net_weight;
$details->save();
return view ('frontend.praiBarcode.viewsupplierdetails')
->with('details',$details);
}
This to check to verify if it is working:
Make sure you are submitting the correct form.
Try doing dd on your controller dd($request->all())
If data is reaching the controller and not inserted into the database, check on your model, if it is added to fillable or if there is only id in the guarded array. You can know about more here in https://laravel.com/docs/9.x/eloquent#mass-assignment
Error should be fixed, as soon as you fix it.
Controller
use Validator;
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'brand' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->with('error', $validator->errors()->first());
}
$details = new SupplierDetail();
$details->brand = $request->brand;
$details->container_no = $request->container_no;
$details->date_received = $request->date_received;
$details->gross_weight = $request->gross_weight;
$details->tare_weight = $request->tare_weight;
$details->net_weight = $request->net_weight;
$details->save();
if ($trending) {
return redirect(route('details.index'))->with('success', 'Field added successfully');
} else {
return redirect()->back()->with('error', 'Field has been not added successfully');
}
}

VueJS Search for exact word or phrase

Im querying data which is pulled in via an Axios call. The drop-down 'subjects' pulls back results and queries the data but I would like it to only pull back exacts. For example, if I select 'English', I just want it to return subjects which have the subjects 'English' and not the subjects which are 'English and maths'.
Would I use a regEx. If so how would I go about this? Any help appreciated.
<select v-model="subject"
class="form-control"
#change="subjectonchange()"
:disabled="selectDisabledSubject"
>
<option disabled value="">Select subject</option>
<option
v-for="subject in uniquesubjects"
:key="subject"
:value="subject"
>
{{ subject }}
</option>
</select>
method: { subjectonchange: function () {
let query = "";
if (this.subject !== "") {
query = this.subject;
console.log(this.subject);
} else {
query = "!showall";
}
this.query(query);
},}

"---please make a choise---" prestashop form

i would like to make default "---please make a choice--'. Actually in checkout adress form state is compilate like italia. I put a screenenter image description here
You need to find block responsible for showing province field and add something like this right above foreach loop:
<option value disabled selected>{l s='-- please choose --' d='Shop.Forms.Labels'}</option>
Example based on "Select a country" located in prestashop\themes\classic\templates_partials\form-fields.tpl:
{block name='form_field_item_country'}
<select
class="form-control form-control-select js-country"
name="{$field.name}"
{if $field.required}required{/if}
>
<option value disabled selected>{l s='-- please choose --' d='Shop.Forms.Labels'}</option>
{foreach from=$field.availableValues item="label" key="value"}
<option value="{$value}" {if $value eq $field.value} selected {/if}>{$label}</option>
{/foreach}
</select>
{/block}

Bootstrap Select - Set selected value by text

How can I change the drop down if I only know the text and not the value?
Here is my select -
<select id="app_id" class="selectpicker">
<option value="">--Select--</option>
<option value="1">Application 1</option>
<option value="2">Application 2</option>
</select>
You can use jquery filter() and prop() like below
jQuery("#app_id option").filter(function(){
return $.trim($(this).text()) == 'Application 2'
}).prop('selected', true);
DEMO
Using BOOTSTRAP SELECT,
jQuery("#app_id option").filter(function(){
return $.trim($(this).text()) == 'Application 2'
}).prop('selected', true);
$('#app_id').selectpicker('refresh');
DEMO
Try this:
$(function(){
// Init selectpicker
$('#app_id').selectpicker({
style: 'btn-info',
size: 4
});
// Set desired text
var optionToSet = "Application 1";
$("#app_id option").filter(function(){
// Get the option by its text
var hasText = $.trim($(this).text()) == optionToSet;
if(hasText){
// Set the "selected" value of the <select>.
$("#app_id").val($(this).val());
// Force a refresh.
$("#app_id").selectpicker('refresh')
}
});
});
Inspired by #Sathish's answer
Working JSfiddle

Datatables - Length (select option) outside datatable

I am using DataTables and I would like my length(select option) to outside of the table
(ex. on my div).
create new select form
<select name='length_change' id='length_change'>
<option value='50'>50</option>
<option value='100'>100</option>
<option value='150'>150</option>
<option value='200'>200</option>
</select>
init dataTables
var oTable = $('#example').DataTable({});
set initial value
$('#length_change').val(oTable.page.len());
add function .change
$('#length_change').change( function() {
oTable.page.len( $(this).val() ).draw();
});
reference : https://datatables.net/reference/api/page.len()
It cannot be directly moved by just copying the whole change length drop down outside the table.
Instead create a new drop-down, where ever you want but set the following in the datatable call -
<select name='length_change' id='length_change'>
<option value='50'>50</option>
<option value='100'>100</option>
<option value='150'>150</option>
<option value=''>All</option>
</select>
`var oTable = $('#sample_1').dataTable( {
.....
"bLengthChange": false, //This will disable the native datatable length change
.....
...
"fnServerParams": function ( aoData ) {
aoData.push( { "name": "length_change", "value": $('#length_change').val() } );
},
.....
....
});
`
The `aoData.push` will send the selected value of the customer length change to the server.
In the Model Class from where the array will be returned for the datatable, include the pushed value to the limit.
i.e. if `$postData` is the array of posted values to the server then -
`if($postData['length_change'])
$limit = (int) $postData['length_change'];
else
$limit = _DEFALUT_VALUE;
`
I hope it helps.