Select2 multiple select passing key,value pairs on postback - asp.net

I am using Select2 Tags using a dropdown as well as a direct input and have the following code in my View:
<select id="lessonTags" class="form-control" multiple="multiple" style="width:100%" name="SelectedTags">
#foreach (var lesson in Model.Lessons)
{
<option value="#lesson.Id">#lesson.Title</option>
}
In my viewmodel I just created a temp variable string[] SelectedTags which gives me the string of what was selected via the dropdown as well as anything that was typed in that was not in my original options.
So currently I have to loop through my data and match to see if the SelectedTags array has a match on my db, if it does then I can use that Id in my model, if it doesn't exist then I have to Create that lesson in my DB.
I find this to be extra processing, it would be nice if I can just pass say a key, value pair of ID/Lesson.Title and anything that I typed wouldn't have an ID so I can easily identify what needs to be created on the DB.
Is there a way to pass the key/value pair via the name tag?
EDIT: Currently whats getting returned is the lesson.Id if its in the list or the actual text if its not - the issue with this is if the user types in a number, i can't discern between an Id and the users number...hence I am thinking I need to somehow flag a new piece of data.

Related

Laravel Form input type="text" select data from another table on type type suggest existing data from the table and select

How can i create a form like the one on linkedin. On form input type when i type is going to fect data from a database if not existis is going to create it and fetch d and store to another table
on form input type, immediately get result: This can be done by AJAX. Laravel support Vue which can do this easily:
1a. call a function in the mounted() part.
1b. the function call itself by putting setTimeout(this.functionName,1000) at the end.
1c. in the function, check if the text in the text box is changed. if yes, make a request by axios.get('blablabla').then(response=>{//put the response into vue dataArrayName in data part});
1d. the html template is auto-updated by v-for="dataArrayName"
looks like the search is done by comparing char by char from the beginning of the string to the data.
if you press enter, pass the search string to the controller, use some logic to determine whether it is needed to create row in database

Remove Blank option on Angular on dynamic option objects

I have this code
<select ng-model='item.data.choice' >
<option ng-if='item.data.simple_allow_blank == false' ng-show='choice.name' ng-repeat='choice in item.data.simple_choices' value="{{choice.name}}" ng-disabled="$parent.controllerAction == 'show'">{{choice.name}}</option>
</select>
It shows the 3 choices entered by the Admin of the system in a dropdown box. It shows it without the blank choice.
The problem is if I want to put a default value (for example, choice[0]), it adds a blank value.
Also when converting from ng-repeat to ng-options from this question.
I have 4 choices 1,2,3 and blank i wanted to remove the blank i followed the guide/answers found on the link i have posted with my question, the difference in my part is that i have a dynamic list and when the list is newly created. it never fails to include the blank when using ng-option but it solves my problem to have an initial default value from the dynamic list, if i use ng-repeat i dont have the initial value but the blank from the choices where remove. i wanted to have the blank choice remove and the same time to have an initial value from the dynamic list.
I can set the default value but it cannot remove the blank option.
Can anyone help? Thanks.
You have to filter data in js file and create a new array that you will render it in html.let say you are getting data in variable **item.data.simple_choices**(as you have written it in ng-repeat)then your function would be.
var data = item.data.simple_choices;
var filteredArray = [];
data.filter(function(item){
if(item.name!=''){
filteredArray.push(item)
}
})
After hours of leeching the web and lots of trial and error I finally got the correct answer.
<select ng-model='model'>
<!-- correct binding -->
<option ng-show='choice.name' ng-repeat='choice in object' value="{{choice.name}}" ng-selected='model = object[0].name'>{{choice.name}}</option>
</select>
The code is simple but this format is helpful because object is an object the has the value of the list. as you can see i did assign it to the model because model is the variable that is being used in the view. The [0] is the index number of the list. You can use value or name depending on what parameters are present on the object.

Symfony2 - Array of data

This is the last problem I am facing with Symfony2. So, I am using a normal form, not the form builder.
I have one table called alert which has the standard fields. I have a second table called booking_class which has an alert key in it which is linked to the alert table. Its a one to many relationship. In my form, I have something like
<input type="text" name="na_booking_class[]">
<input type="text" name="na_booking_class[]">
So this input is placed into an array. Now in my controller, I presume that this will give me my array of data
$alert->setBookingClass($request->get('na_booking_class'));
However, setBookingClass only accepts a String. So I need to somehow loop this array and pass the String values to setBookingClass.
How would this be achieved?
Thanks
If you get an array from this $request->get('na_booking_class'); then you can use:
$arrBooking = $request->get('na_booking_class');
foreach($arrBooking as $booking) {
$alert->setBookingClass($booking);
}
Try with this.

getting value of inputs with suffix in name attribute - Classic ASP

I'm trying to write a Form Builder system.
Now, i reached to gathering information section and at this section i need to grouping the information to three group like:
"Short-Response" for text inputs
"Long-Response" for textarea
and "Option-Response" for radio, checkbox and dropdown
in Form page view, system create the form, based on database records.
I'm using suffix for inputs based on those group as i said earlier, like these:
<inputs name="dynamicpart_short" value="" />
<textarea name="dynamicpart_logn"></textarea>
Now, In server side i want to gather these information and group them.
I'm using
for each item in Request.Form
Next
But how i can create a loop based on inputs suffix like "_short" or "_long" and ....
means each suffix has it's own loop!
PS:
inputs have a name with two parts, first part is dynamic and is a random numbers!
Like this: name="15_short"
"item" will have the name of the form field, use a string function to check if it has short or long in it:
for each item in Request.Form
if instr(item,"_short")<>0 then
'do something
elseif instr(item,"_long")<>0 then
'do something else
'.....
end if
Next

Asp.Net, Dynamic Data Website, Foreign key One to One relationship, DropdownList, how to customise?

I'm using Asp.Net's Dynamic Data Website project type.
I've created a db that has one to onerelationship between two tables specified using a foreign key, (this could be one to many but I've used the linq designer to change it to a one to one relationship).
i.e. I have a table Invoice and table CreditNote that takes an Invoice_Id as a foreign key
I was hoping that when I navigated to CreditNotes/Insert.aspx that it would display the a list of InvoiceIds in a dropdown list. It almost does this - it creates dropdownlist but only populates option value and not the text so I get something like this in the html source (notice only the value is populated - which looks empty to the user):
<select>
<option value="someInvoiceId"></option>
<option value="someInvoiceId"></option>
</select>
Can anyone tell me how I could fix this?
Thanks in advance for any help!
Hey I just stumbled over this question look for another answer but I think that you might need to set up some more meta data on your model such as the adding attributes so that you can define which field should be used
http://ericphan.info/blog/2009/2/24/aspnet-dynamic-data-display-custom-text-in-a-foreign-key-dro.html

Resources