how to validate dropdown optionlist required functionality asp.net mvc - asp.net-mvc-2

I have dropdown like this ,
<%= Html.OptionList("Worktype", new SelectList(new List<SelectListItem>{
new SelectListItem{Text = "--Select One--", Value = "0", Selected=true},
new SelectListItem{Text = "Fulltime", Value = "Full Time"},
new SelectListItem{Text = "Partime", Value = "Part Time"}}, "Value", "Text" )) %>
After selecting either fulltime or parttime it should submit, but because the default select is there, required validation is passing. I want the required validation for below two options. can anyone help me out.
thank you,
michael

SetValue empty instead of 0 for "--Select One--"
new SelectListItem{Text = "--Select One--", Value = string.Empty , Selected=true}

I suggest that you should not be adding optional label in SelectList or as SelectListItem. you have overloads for Html.DropDown and Html.DropDownListFor that would allow you to insert an optional label at the top of your dropdown list. Pleas check my answer to this question.

DO you want to fire any event only in case of full time and part time and in case of select you dont want anything to happen.
If this is what you want
$('#dropdownname').change(function () {
var dropdownValue = $(this).val();
if (dropdownValuetoString().length > 0)
{
Your Code here.........
}
});
dropdownname is the name of dropdown dropdownValue is what I m getting from dropdown list when index is changed.
I was filling the dropdown from a list and I was not using any value field
when u check the dropdownValue for select It will show blank and I m sure ur dropdown select list will always have a name.
Tell me if it helps you else I will try something different

Related

Dynamic form Issue in Angular

Scenario :
A form is generated dynamically, I used this tutorial to create the form dynamically. When I click "Submit" button, the form value I get in the component is
ts :
doSubmitICR(form) {
console.log(form.value);
}
form.value has the following values
{
CaptainPoint:"0",
Category:"restaurant",
Description:"Test",
DisplayDept:"kitchen",
Group:"restaurant",
GuestPoint:"0",
ItemCode:"1",
ItemName:"Test Item",
PrintingDept:"frontoffice",
SalesRate:"3",
SubGroup:"restaurant",
TAX1:true,
TAX2:true,
TabDisplay:"restaurant",
Unit:"restaurant"
}
TAX1 and TAX2 are dynamic, those form controls were created from a tax table, If the table has TAX3 and TAX4, then instead of TAX1 and TAX2 in the above form, TAX3 and TAX4 form control is generated.
Issue :
If the TAXs are dynamic, how can I pass these values to Web service say WebAPI2 as a model?
Is it possible to filter only the TAX element from the form.value?
Any advice would be appreciated. Thank you.
The solution from alexKhymenko should work well. Alternatively, if the values you want to submit to the API will always have the format of TAX{d}, where d is some combination of digits, you could use the following to get just those values on a request object:
let request = {};
Object.keys(form.value).forEach(key => {
if(/^TAX[\d]+$/.test(key))
request[key] = form.value[key]
});

Check if text binded to a control is null

I'm trying to check if I'm binding a null data on a controller. If the data is null, I need to not show the label as well as the binded data.
Below is my code right now.
var oMatNrRow1 = new sap.ui.commons.layout.MatrixLayoutRow();
control1 = new sap.ui.commons.Label({
text : Appcc.getText("MATERIAL_NO") + ":"
});
matrixCell1 = new sap.ui.commons.layout.MatrixLayoutCell();
matrixCell1.addContent(control1);
control = new sap.ui.commons.Label();
control.bindProperty("text", "matnr");
matrixCell1.addContent(control);
oMatNrRow1.addCell(matrixCell1);
vendorTable.addRow(oMatNrRow1);
I have tried control.getProperty("text") but it only returns null when it should have return a number if matnr is not null.
I also tried formatter. I will have no problem with formatter if matnr is not null. But if it is null, the point is to destroy/delete contents of both matrixCell1 instances. In my code below, addition of matrixCell1 content will still push through.
...
formatter: function(matnr){
if (matnr !== ''){
return contract
} else{
matrixCell.destroyContent();
}
});
matrixCell1.addContent(control);
oMatNrRow1.addCell(matrixCell1);
vendorTable.addRow(oMatNrRow1);
Not sure if you can move the ff code inside if statement
matrixCell1.addContent(control);
oMatNrRow1.addCell(matrixCell1);
vendorTable.addRow(oMatNrRow1);
Any ideas are appreciated.
I would also suggest to user the visible property.
Are you aware of conditional binding of UI5? Using them you do not need the formatter at all in that case. see
Found a workaround on my issue. It was a simple if else condition. For the if statement, I just added data[j].matnr and it worked! I also noticed that this was how SAP implemented the behavior also e.g. oSearchViewData.description.

Lift - how to get default value of select using WiringUI

I have an issue with getting default value of select dropdown.
i have fruits val:
val fruits = List("apple", "banana", "other")
and i render a tr with:
<tr id={ theLine.guid }>
<td>
{
SHtml.ajaxSelect(fruits, Full(fruits(0)),
s => {
mutateLine(theLine.guid) {
l => Line(l.guid, l.name, s, l.note)
}
Noop
})
}
</td>
(...)
on page html is rendered correctly with option selected="selected", but when i try to save it to DB i get empty value of fruit. if i change option to 'other' and then i select it back to 'apple', it saves right value.
i add also a println function to addLine to see what values are in this vector, and there is empty value if i dont change fruit option, so i suppose that it is not problem when i process lines to save it to DB.
can you help me with this?
thanks
Gerard
Before you change your select option, you are not triggering the event that calls your function. The function is bound to onChange and that only gets fired when the value changes.
To fix, you could either: Start with an option like "Select a value". This would require the user to change the item, but is the only way to trigger the onchange.
If you don't want to do that, you could add a button and add your logic to a button click handler that would get called when submitted. Something like this should help - you'll need to bind it to your output, either inline as you provided, or via CSS Selectors:
var selected = Full(fruits(0))
SHtml.ajaxSelect(fruits, selected,
s => {
selected = Full(s)
Noop
})
SHtml.ajaxSubmit("Submit", () => {
mutateLine(theLine.guid) {
l => Line(l.guid, l.name, selected, l.note)
}
})

jqgrid edittype select load value from data

I am using jqgrid in my new project.
In a specific case I need to use a select element in the grid. No problem.
I define the colModel and the column for example like (from wiki)
colModel : [
...
{name:'myname', edittype:'select', editoptions:{value:{1:'One',2:'Two'}} },
...
]
But now when I load my data I would prefer the column "myname" to contain the value 1.
This won't work for me instead it has to contain the value "One".
The problem with this is that the text-part of the select element is in my case localized in the business layer where the colModel is dynamically generated. Also the datatype for the entity which generates the data via EF 4 may not be a string. Then I have to find the correct localized text and manipulate the data result so that the column "myname" does not containt an integer which is typically the case but a string instead with the localized text.
There is no option you can use so that when the data contains the value which match an option in the select list then the grid finds that option and presents the text.
Now the grid presents the value as a text and first when I click edit it finds the matching option and presents the text. When I undo the edit it returns to present the value again.
I started to think of a solution and this is what I came up with. Please if you know a better solution or if you know there is a built in option don't hesitate to answer.
Otherwise here is what I did:
loadComplete: function (data) {
var colModel = grid.getGridParam('colModel');
$.each(colModel, function (index, col) {
if (col.edittype === 'select') {
$.each(grid.getDataIDs(), function (index, id) {
var row = grid.getRowData(id);
var value = row[col.name];
var editoptions = col.editoptions.value;
var startText = editoptions.indexOf(value + ':') + (value + ':').length;
var endText = editoptions.indexOf(';', startText);
if (endText === -1) { endText = editoptions.length; }
var text = editoptions.substring(startText, endText);
row[col.name] = text;
grid.setRowData(id, row);
});
}
});
}
It works and I will leave it like this if nobody comes up with a better way.
You should just include additional formatter:'select' option in the definition of the column. See the documentation for more details.

Dijit/Dojo Inline - Filtering Select - DataStore

I am using an inline filteringselect with datastore, as follows:
I am using ABBR as the identifier and NAME as the value.
The filtering selects and works correctly, but I have two issues.
Firstly, how do I retrieve the ABBR for the selected option NAME?
I have tried various things, including .innerHTML but that only retrieves the selected item name, not the identifier.
Secondly, when using the datastore option, how can I choose the default selected item, for example if it was a scale of 1 to 10 and I wanted 5 as the default selection, how can I do this?
Any ideas and advice would be greatly appreciated.
Mank thanks
dojo.addOnLoad(function() {
// inline store
str = new dojo.data.ItemFileReadStore({data: storeData10})
var itmes;
// for storing the store's items
str.fetch({
onComplete:function(itms){
itmes= itms;
console.log(itms)
}
})
dijit.byId("cmbx1").store = str
dojo.connect(dijit.byId("cmbx1"), 'onChange',function(){
//console.log(arguments);
//get the value u c in screen
var whatvseeinselect = dijit.byId("cmbx1").focusNode.value;
dojo.forEach(itmes, function(itm){
//compare the value u c in screen with store itms. once matched take that item and get the name attr or other attr if u require..
if(whatvseeinselect == str.getValue(itm,"name")){
console.log(str.getValue(itm,"name"));
}
})
})
});
I'm not sure whether this is the correct way.
Hope this helps