Introduction: In this
article today I am going to explain how to filter the records in MVC
Description:
In this example I and shown detail of
register users and want to filter the records on the username. To implement the
search functionality follows the given steps:
I have a class file in Models (User_Registration.cs) and having
structure as shown below:
using System.ComponentModel.DataAnnotations;
using
System.ComponentModel.DataAnnotations.Schema;
public class User_Registration
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="Please Enter
UserName")]
public string User_Name { get; set; }
[Required(ErrorMessage = "Please Enter First
Name")]
public string First_Name { get; set; }
[Required(ErrorMessage = "Please Enter Last
Name")]
public string Last_Name { get; set; }
[DataType(DataType.Upload)]
public string Profile_Image { get; set; }
[Required(ErrorMessage = "Please Enter
Password")]
public string Password { get; set; }
[RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",ErrorMessage = "Please Enter Email
ID")]
public string EmailId { get; set; }
[Required(ErrorMessage = "Please Enter
Gender")]
public string Sex { get; set; }
[Required(ErrorMessage
= "Please
Select Qualification")]
[ForeignKey("Master_Qualification")]
public int EducationId { get; set; }
[Required(ErrorMessage = "Please Select
Country")]
[ForeignKey("Master_Country")]
public int CountryId { get; set; }
[Required(ErrorMessage = "Please Enter Phone
No")]
public int Phone_No { get; set; }
[Required(ErrorMessage = "Please Select the
terma & Conditions")]
public bool Terms { get; set; }
public virtual Master_Country Master_Country { get; set; }
public virtual Master_Qualification Master_Qualification { get; set; }
}
I want to show the users detail and want to
filter the records on username. Write the below given code in Controller show
the details and filter the records.
Controller:
private ProjectContext db = new ProjectContext();
public ActionResult Index(string search)
{
var user_registrations =
db.User_Registrations.Include(u => u.Master_Country).Include(u =>
u.Master_Qualification);
if(!String.IsNullOrEmpty(search))
{
user_registrations = user_registrations.Where(s
=> s.User_Name.Contains(search));
}
return View(user_registrations.ToList());
}
Add the view for Controller and design as
code given below.
View:
@model IEnumerable<UserRegistration.Models.User_Registration>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "UserRegistration",
FormMethod.Get))
{
//textbox to filter
the records
<p> UserName:
@Html.TextBox("search") <input type="submit" value="Search" /></p>
}
</p>
<table
class="table">
<tr>
<th>
UserName
</th>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Profile Image
</th>
<th>
EmailId
</th>
<th>
Sex
</th>
<th>
Qualification
</th>
<th>
Country Name
</th>
<th>
Phone No
</th>
<th>
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr style="border-right:1px solid #000;">
<td>
@Html.DisplayFor(modelItem =>
item.User_Name)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.First_Name)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.Last_Name)
</td>
<td>
<img src="@Url.Content(item.Profile_Image)" alt="Image" height="100px" width="100px" />
</td>
<td>
@Html.DisplayFor(modelItem =>
item.EmailId)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.Sex)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.Master_Qualification.Qualification)
</td>
<td>
@Html.DisplayFor(modelItem => item.Master_Country.Country_Name)
</td>
<td>
@Html.DisplayFor(modelItem =>
item.Phone_No)
</td>
<td>
@Html.ActionLink("Edit",
"Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new {
id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new {
id=item.Id })
</td>
</tr>
}
</table>
Now build the project and run. Check the
result.
No comments:
Post a Comment