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
sangeetha k
NA
207
51.6k
search functionaliity in mvc , its fetching but not binding
Apr 23 2018 5:32 AM
#dal
public
List<tblEmployee> SearchRecord(
string
searchBy,
string
txtSearch)
{
try
{
EmployeeEntity emp =
new
EmployeeEntity();
using
(AdventureWorksEntities empDbentity =
new
AdventureWorksEntities())
{
if
(searchBy ==
"Empid"
)
{
var a =
int
.Parse(txtSearch);
return
empDbentity.tblEmployees.Where(x => x.Empid == a || txtSearch ==
null
).ToList();
}
else
{
//var search = Convert.ToInt32(txtSearch);
return
empDbentity.tblEmployees.Where(x => x.Empname.StartsWith(txtSearch) || txtSearch ==
null
).ToList();
}
}
}
catch
(Exception ex)
{
Log.WriteError(
"BorrowerImageGridView.EmployeeDataAcess.searchRecord()"
, ex);
throw
new
Exception(TUtil.GetMethodExceptionContext(searchBy, txtSearch), ex);
}
#controller
[HttpGet]
public
ActionResult SearchRecord(
string
searchBy,
string
txtSearch)
{
List<tblEmployee> emp = empservice.SearchRecord(searchBy, txtSearch);
return
View(
"EmployeeDetails"
, emp);
}
# my view
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<EmployeeEntityLayer.EmployeeEntity>
@{
Layout =
null
;
ViewBag.Title =
"EmployeeDetails"
;
}
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>EmployeeForm</title>
<link rel=
"stylesheet"
href=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"
></script>
<script src=
"http://code.jquery.com/jquery-3.3.1..min.js"
></script>
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
></script>
<script src=
"~/Scripts/jquery-3.3.1.js"
></script>
<script src=
"https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"
type=
"text/javascript"
></script>
<link href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
rel=
"stylesheet"
/>
<style>
.Delete {
color: green;
}
.table td {
width: 25%;
}
.edit {
color: blue;
}
</style>
</head>
<body>
<div
class
=
"panel panel-info margin"
>
<div
class
=
"panel-heading"
><b>Registered Employess</b></div>
<div
class
=
"panel-body"
>
<div >
<div
class
=
"input-group"
>
<b>Search By:</b>
@Html.RadioButton(
"SearchBy"
,
"Empname"
) <text>Empname</text>
@Html.RadioButton(
"SearchBy"
,
"Empid"
) <text>Empid</text>
@Html.TextBox(
"txtSearch"
,
""
,
new
{ @id =
"txtSearch"
, @
class
=
"form-control"
, style =
" margin-bottom: 25px ;align-content:center; font-family:Arial, FontAwesome"
, placeholder =
"Search"
})
@*<input type=
"text"
classEmpl=
"form-control"
id=
"txtSearch"
placeholder=
"Search for..."
>*@
<span
class
=
"input-group-btn"
>
<button id=
"btnSearch"
class
=
"btn btn-secondary"
style=
"margin-bottom:4px;"
type=
"submit"
><i
class
=
"glyphicon glyphicon-search"
></i></button>
</span>
</div>
@*<input id=
"txtSearch"
style=
" margin-bottom: 25px ;font-family:Arial, FontAwesome"
type=
"text"
placeholder=
" ? Search "
name=
" search"
>
<button type=
"button"
class
=
"btn btn-info"
style=
" margin-bottom: 25px"
>
<span
class
=
" glyphicon glyphicon-search"
>
</span> Search
</button>*@
</div>
@*<div
class
=
"input-group"
>
<input style=
" margin-bottom: 25px"
type=
"text"
class
=
"form-control"
placeholder=
"Search"
/>
<div
class
=
"input-group-btn"
>
<i
class
=
"glyphicon glyphicon-search"
></i>
</div>
</div>*@
@using (@Html.BeginForm(
"MultiDeleteemployee"
,
"Employee"
, FormMethod.Post))
{
<div
class
=
"col-sm-12 container"
>
<div>
<table id=
"EmpTable"
class
=
"table table-striped table-responsive table-bordered"
style=
"width:100%"
>
<thead>
<tr>
<td></td>
<th>EmpLoyee ID</th>
<th>Employee Name</th>
<th>Designation</th>
<th>Employee Image</th>
<th>Salary</th>
<th>Email</th>
<th>Department</th>
<th></th>
</tr>
</thead>
<tbody id=
"myTableBody"
>
@foreach (
var
item
in
Model)
{
<tr>
<td>@Html.CheckBox(
"chkDelteBox"
,
new
{ @id =
"chkDeleteBox"
, data_id =
"@item.Empid"
})</td>
<td scope=
"row"
> @Html.DisplayFor(model => item.Empid)</td>
<td> @Html.DisplayFor(model => item.Empname)</td>
<td> @Html.DisplayFor(model => item.Designation)</td>
@*<td>
<img src=
"@Url.Content(@item.Empimage)"
alt />
</td>*@
<td>
@{
var
base64 = Convert.ToBase64String(item.ImageData);
var
imagesrc = string.Format(
"data:image/jpeg;base64,{0}"
, base64);
<img src=
'@imagesrc'
style=
"max-width:100px;max-height:100px;"
/>
}
</td>
<td>@Html.DisplayFor(model => item.Salary,
new
{ @
class
=
"edit"
})</td>
<td> @Html.DisplayFor(model => item.Email)</td>
<td>@Html.DisplayFor(model => item.Department)</td>
<td>
<a href=
"#"
data-id=
"@item.Empid"
id=
"btnDelete"
class
=
"Delete"
role=
"button"
>
<span
class
=
"glyphicon glyphicon-trash"
></span>
</a>
</td>
<td>
<a href=
"#"
data-id=
"@item.Empid"
id=
"btnEdit"
class
=
"edit"
role=
"button"
data-toggle=
"modal"
data-target=
"#EditModals"
>
<span
class
=
"glyphicon glyphicon-edit"
></span>
</a>
</td>
</tr>
}
</tbody>
</table>
@Html.PagedListPager(Model, page => Url.Action(
"EmployeeDetails"
,
new
{ page }))
<input type=
"submit"
id=
"btnback"
value=
" GetBack"
class
=
"btn btn-default"
/>
<input type=
"button"
id=
"btnMultiDelete"
value=
" DeleteRecords"
class
=
"btn btn-default"
/>
</div>
</div>
}
</div>
</div>@using (Html.BeginForm(
"UpdateEmployee"
,
"Employee"
, FormMethod.Post,
new
{ enctype =
"multipart/form-data"
, @
class
=
"form-horizontal"
, role =
"form"
}))
{
<div
class
=
"modal fade"
id=
"EditModals"
role=
"dialog"
>
<div
class
=
"modal-dialog"
style=
"padding-right: 16px; margin-right: 200px; margin-right: 74px; left: 148px; margin-left: 127px; padding-left: 11px; "
>
<div
class
=
"modal-content"
>
<div
class
=
"modal-header"
>
<button
class
=
"close"
aria-hidden=
"true"
type=
"button"
data-dismiss=
"modal"
>X</button>
<h4
class
=
"modal-title"
>Edit Employee Details</h4>
</div>
<div
class
=
"modal-body"
>
<div style=
"text-align:center"
>
<span id=
"errmsg"
style=
"color:red;"
></span>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
EmpLoyee ID:@Html.TextBox(
"txtEmpId"
,
""
,
new
{ @id =
"txtEmpId"
, @readonly =
"readonly"
, @
class
=
"form-control"
, style =
"align-content:center"
})
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
Employee Name: @Html.TextBox(
"txtEmpName"
,
""
,
new
{ @id =
"txtEmpName"
, @
class
=
"form-control"
, style =
"align-content:center"
})
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
Designation: @Html.TextBox(
"txtDesignation"
,
""
,
new
{ @id =
"txtDesignation"
, @
class
=
"form-control"
, style =
"align-content:center"
})
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
<div>
<img id=
"user_img"
height=
"100"
width=
"90"
style=
"border:solid"
align=
"center"
/>
</div>
Employee Image: @Html.TextBox(
"txtEmpimage"
,
" "
,
new
{ type =
"file"
, @id =
"txtEmpimage"
, @
class
=
"form-control"
, @onchange =
"show(this)"
})
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
Salary: @Html.TextBox(
"txtSalary"
,
" "
,
new
{ @id =
"txtSalary"
, @
class
=
"form-control"
})
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
Email: @Html.TextBox(
"txtEmail"
,
" "
,
new
{ @id =
"txtEmail"
, @
class
=
"form-control"
})
</div>
</div>
<div
class
=
"form-group"
>
<div
class
=
"col-sm-12"
>
Department: @Html.TextBox(
"txtDepartment"
,
" "
,
new
{ @id =
"txtDepartment"
, @
class
=
"form-control"
})
</div>
</div>
</div>
<div
class
=
"modal-footer"
>
<div
class
=
"form-group"
>
<button type=
"submit"
id=
"btnUpdate"
class
=
"btn btn-success"
data-toggle=
"modal"
data-target=
"#myModal"
>UPDATE</button>
<button type=
"button"
id=
"btnCancel"
class
=
"btn btn-danger"
data-dismiss=
"modal"
>Cancel</button>
</div>
</div>
</div>
</div>
</div>
}
<div
class
=
"modal fade"
id=
"myModal"
role=
"dialog"
>
<div
class
=
"modal-dialog"
>
<!-- Modal content-->
<div
class
=
"modal-content"
>
<div
class
=
"modal-header"
>
<button type=
"button"
class
=
"close"
data-dismiss=
"modal"
>×</button>
</div>
<div
class
=
"modal-body"
>
<p>Updation Successfull..</p>
</div>
<div
class
=
"modal-footer"
>
<button type=
"button"
class
=
"btn btn-default"
data-dismiss=
"modal"
>Close</button>
<button type=
"submit"
id=
"btnOk"
class
=
"btn btn-success"
>ok</button>
</div>
</div>
</div>
</div>
@section scripts{
<script src=
"https://www.appelsiini.net/download/jquery.jeditable.js"
></script>
<script>
$(document.ready(
function
() {
$(
"#EmpTable"
).on(
'click'
,
'.Delete'
, (
function
() {
var
empID = $(
this
).attr(
'data-id'
);
var
oldValue =
''
;
$(
'.edit'
).editable(
'/Employee/UpdateOrEditEmployee?id='
+ empID, {
cssclass:
'jeditForm'
,
tooltip:
'Click to edit me....'
,
width:
'none'
,
height:
'none'
,
onsubmit:
function
(settings, original) {
oldValue = original.revert;
},
submitdata:
function
() {
return
{
id: $(
this
).data(
'Empid'
),
PropertyName: $(
this
).data(
'propertyname'
)
}
},
callback:
function
(value, settings) {
var
jsonData = $.parseJSON(value);
if
(jsonData.status) {
$(
this
).text(jsonData.value);
}
else
{
$(
this
).text(oldValue);
}
}
})
}))
}))
</script>
}
<script>
//showing the image uploaded from the file uploader
function
show(input) {
if
(input.files && input.files[0]) {
var
filerdr =
new
FileReader();
filerdr.onload = FileReader();
filerdr.onload =
function
(e) {
$(
'#user_img'
).attr(
'src'
, e.target.result);
}
filerdr.readAsDataURL(input.files[0]);
}
}
$(document).ready(
function
() {
// deleteing a record using the icon click.
$(
"#EmpTable"
).on(
'click'
,
'.Delete'
, (
function
() {
var
empID = $(
this
).attr(
'data-id'
);
window.location.href =
"/Employee/DeleteEmployee?empid="
+ empID;
}));
//deleting multiple records
//$("#btnMultiDelete").click(function () {
// debugger;
// var item ;
// var empID = $(this).attr('data-id');
// window.location.href = "/Employee/MultiDeleteemployee?empid=" + empID;
//});
btnDelete
$(
"#btnUpdate"
).click(
function
() {
window.location.href =
"/Employee/UpdateEmployee"
;
});
//$("#btnSearch").click(function () {
// var selectedvalue = $('input= searchBy]:checked', '#myform').val();
// var typedvalue = $("#txtSearch").val();
// window.location.href = "/Employee/SearchRecord?searchBy=" + selectedvalue & " txtSearch=" + typedvalue;
//});
$(
"#btnback"
).click(
function
() {
window.location.href =
'@Url.Action("Employee", "Employee")'
;
});
$(document).on(
'click'
,
'#btnMultiDelete'
,
function
() {
debugger
;
var
empid = $(
'#chkDeleteBox'
).attr(
'data-id'
);
$.ajax({
type:
'POST'
,
url:
'@Url.Action("MultiDeleteemployee", "Employee")'
,
data:
"{Empid:'"
+ empid +
"'}"
,
contentType:
"application/json;charset=utf-8"
,
datatype:
"json"
,
sucess:
function
(data) {
alert(data);
},
error:
function
(data) {
alert(
'error'
);
}
});
});
// ajax call for editing a record.
$(document).on(
'click'
,
'#btnEdit'
,
function
() {
debugger
;
var
empID = $(
this
).attr(
'data-id'
);
$.ajax({
type:
'POST'
,
url:
'@Url.Action("EditEmployee","Employee")'
,
data:
"{Empid:'"
+ empID +
"'}"
,
contentType:
"application/json;charset=utf-8"
,
dataType:
"json"
,
success:
function
(data) {
if
(data == 0 || data ==
null
) {
debugger
;
data.length == 0;
//clear();
$(
'#txtEmpId'
).val();
$(
'#txtEmpName'
).val();
$(
'#txtDesignation'
).val();
$(
'#txtEmpimage'
).val();
$(
'#txtSalary'
).val();
$(
'#txtEmail'
).val();
$(
'#txtDepartment'
).val();
}
else
if
(data.length != 0 && data != 0) {
debugger
;
var
Empid = data[0].Empid;
var
Name = data[0].Empname;
var
Designation = data[0].Designation;
var
Empimage = data[0].Empimage;
var
Employeesalary = data[0].Salary;
var
Department = data[0].Department;
var
Emailid = data[0].Email;
if
(Empid !=
null
) {
$(
'#txtEmpId'
).val(Empid);
}
if
(Name !=
null
) {
$(
'#txtEmpName'
).val(Name);
}
if
(Designation !=
null
) {
$(
'#txtDesignation'
).val(Designation);
}
if
(Empimage !=
null
) {
$(
'#txtEmpimage'
).val(Empimage);
}
if
(Employeesalary !=
null
) {
$(
'#txtSalary'
).val(Employeesalary);
}
if
(Department !=
null
) {
$(
'#txtDepartment'
).val(Department);
}
if
(Emailid !=
null
) {
$(
'#txtEmail'
).val(Emailid)
}
}
//$('#EditModals').modal('show');
},
failure:
function
(data) {
alert(
"Failed"
);
},
error:
function
(data) {
alert(
"error"
);
}
});
});
///Searching a Record In client Side
// var $rows = $("#myTableBody tr");
// $('#txtSearch').keyup(function () {
// var val = $.trim($(this).val()).replace(/ +/g, '').toLowerCase();
// $rows.show().filter(function () {
// var text = $(this).text().replace(/\s+/g, '').toLowerCase();
// return !~text.indexOf(val);
// }).hide();
// });
//});
$(document).on(
'click'
,
'#btnSearch'
,
function
() {
debugger
;
var
selectedItem = $(
"input[name=SearchBy]:checked"
).val();
$.ajax({
url:
'/Employee/SearchRecord'
,
data: { searchBy: selectedItem,txtSearch: $(
'#txtSearch'
).val() },
type:
"get"
,
sucess:
function
(response) {
}
});
this
is my ajax call that is used to redirect to that controller method when clicked the search button
});
});</script>
</body>
</html>
someone kindly help me it is fetching me the data but its not changing the view .
Reply
Answers (
6
)
Creating Crystal Report
Resize Control using mousedown and mouseup event dynamically