Skip to main content

Working with select or drop-down using jQuery

In this article we are going to learn some operation on the select box using jQuery as below.

1. Add new option to a select-element with jQuery


<select id="ddlStatus"></select>

<script type="text/javascript" language="javascript" >
var status = new Array();

status.push("<option value=\"In Process\">In Process</option>");
status.push("<option value=\"On Hold\">On Hold</option>");

$("#ddlStatus").html(status.join(''));

</script>

2. Retrieve the selected single value from the select box with jQuery


$('#ddlStatus').val();

3. Retrieve the multiple selected value from the select box with jQuery


$('#ddlStatus option:selected').each(function(counter, option){
console.log($(option).val());
});

4. Sometime if you don’t want to retrieve selected value and want to get selected text then use the below code


$('#ddlStatus option:selected').text();

5. Retrieve the multiple selected text from the select box with jQuery

If you have a multiple choice element you should go with this code:

$('#ddlStatus option:selected').each(function(counter, option){
console.log($(option).text());
});

6. If you want to remove a specific option from the select box using jQuery

Below code will remove the option which has the value = 2 from the using jQuery


$('#ddlStatus option[value=2]').remove();

7. Clear a selectbox using jQuery

$('#ddlStatus').html('');

hope this will help someone 🙂

How to get all model validation errors in ASP.NET MVC?

In ASP.NET MVC you can get a list of all validation errors for your model from the ModelState

if (!ModelState.IsValid)
{
var modelErrors = new List<string>();
foreach (var modelState in ModelState.Values)
{
foreach (var modelError in modelState.Errors)
{
modelErrors.Add(modelError.ErrorMessage);
}
}
// Here you have the list of error, you can show them on the view or generate a erro string and display it using javascript.
}

If you want to display it in razor then you can use @Html.ValidationSummary().

Hope it will help to find the actual error from the modal state.

Explore Action Filters in Asp.Net MVC Applications

What is Action Filters

In the Asp.Net MVC application there are controllers, each controller defines action methods that generally have one to one relation ship with views or each actions use performs on the page.
User actions means we can say like clicking on buttons, links or submitting a form. When this kind of action performs request is routed to controller and finds the action method.
Now if we want to perform some action, calculate login or check some values either before calling an action method or after action method completes Action Filters are used.

Asp.Net MVC provides actions filters which we can say custom attributes which provides facility to perform some behavior at pre-action or post-action time of controller action method.

Now lets see the available types of the Action filter in Asp.Net MVC

Action Filter Types

Asp.Net MVC provides 4 types of action filters like Authorization filter, Action filter, Result filter and Exception filter. Please find the below description for each type of action filter.

Authorization filter

Generally it is used to implement authorization and authentication for action method, It is related to security of the application whether to execute action method or not, it also check for the authentication and request property.
As an example you can see AuthorizeAttribute class for Authorization action filter.

Action filter

Generally it is used to perform some extra processing, like before executing any action method we want to check like user is logged in or not, or user has enough permission to execute this action method or not.
If session expired and we want to transfer request to login page etc.

Result filter

Generally it is used to perform extra processing of the result like modifying the response that means if we want to execute a login before or after the view result get executes we can use this filters.
As an example you can see OutputCacheAttribute class for result filter.

Exception filter

It executes when ever there is an unhanded exception thrown by anywhere in the action method. Exception filters can be used for the tasks like logging an error or displaying an error page 404
As an example you can see HandleErrorAttribute class for exception filter.