TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ramesh rammi
NA
86
14.1k
How to add new option if not exist in the list using select2
Dec 25 2019 7:36 AM
Actually i had implement the multiple values jquery autocomplete textbox.if the option or item not found in the list i want to add that option to autocomplete box
Autocomplete.cshtml:
@{
Layout =
null
;
}
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>Select2
with
Ajax</title>
<link href=
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
rel=
"stylesheet"
/>
<link href=
"https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css"
rel=
"stylesheet"
/>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"
></script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"
></script>
</head>
<body>
<div
class
=
"container"
>
<div
class
=
"form-group"
>
<label
for
=
"Select Country"
>Select Country</label>
<select multiple=
"multiple"
class
=
"chose-country form-control"
></select>
</div>
</div>
<script>
$(document).ready(
function
() {
$(
".chose-country"
).select2({
ajax: {
url:
'/Home/GetEmployeeList'
,
width:
'resolve'
,
data:
function
(params) {
return
{
q: params.term
// search term
};
},
processResults:
function
(data) {
return
{
results: data.items
};
},
minimumInputLength: 2,
width:
'resolve'
}
});
});
</script>
</body>
</html>
Select2Model:
public
class
Select2Model
{
public
string
id {
get
;
set
; }
public
string
text {
get
;
set
; }
}
HomeController:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
WebApplication3.Models;
namespace
WebApplication3.Controllers
{
public
class
HomeController : Controller
{
public
ActionResult Index()
{
return
View();
}
public
ActionResult About()
{
ViewBag.Message =
"Your application description page."
;
return
View();
}
public
ActionResult Contact()
{
ViewBag.Message =
"Your contact page."
;
return
View();
}
public
ActionResult autocomplete()
{
return
View();
}
public
ActionResult GetEmployeeList(
string
q)
{
var list =
new
List<Select2Model>();
list.Add(
new
Select2Model()
{
text =
"India"
,
id =
"101"
});
list.Add(
new
Select2Model()
{
text =
"Srilanka"
,
id =
"102"
});
list.Add(
new
Select2Model()
{
text =
"Singapore"
,
id =
"103"
});
if
(!(
string
.IsNullOrEmpty(q) ||
string
.IsNullOrWhiteSpace(q)))
{
list = list.Where(x => x.text.ToLower().StartsWith(q.ToLower())).ToList();
}
return
Json(
new
{ items = list }, JsonRequestBehavior.AllowGet);
}
}
}
Actually i had implement the multiple values jquery autocomplete textbox.if the option or item not found in the list i want to add that option to autocomplete box
Please help me.
thanks && Regards
Reply
Answers (
1
)
validate vehicle no.
Restrict input with regex