Hy Friends,
I'm newbie in ASP.Net MVC
I need some help with search and reset (fill back list) in ASP.Net MVC
This is my index. cshtml, when I click search, it works normally as what I want but
when I push button Reset, it should will display all the data and clear the search box
Does anyone could help me ?
Thank You
This is my index:
@默 IEnumerable<SearchSortPaging.Models.Student>
@{
ViewBag.Title = "Index";
}
@* Html.BeginForm is used to add a form in an Html document @
@ Pass the action name in the first parameter, controller name in the second parameter and specify the form request type as get *@
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
//the following are search options
<b> Search Option: </b>
@azzedinehtmlsql .RadioButton("option", "Name")
<text>Name</text> @azzedinehtmlsql .RadioButton("option", "Gender")
<text> Gender </text>
@azzedinehtmlsql .RadioButton("option", "Subjects")
<text>Subject</text> @azzedinehtmlsql .TextBox("search")
<input type="submit" name="submit" value="Search" />
<input type="submit" name="submit" value="Reset" asp-action="Reset"/>
}
}
<table class="table">
<tr>
<th>
@azzedinehtmlsql .DisplayNameFor(model => model.Name)
</th>
<th>
@azzedinehtmlsql .DisplayNameFor(model => model.Gender)
</th>
<th>
@azzedinehtmlsql .DisplayNameFor(model => model.Subjects)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subjects)
</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>
And this is my Controller:
namespace SearchSortPaging.Controllers
{
public class HomeController : Controller
{
StudentsDBContext db = new StudentsDBContext();
// GET: Home
public ActionResult Reset()
{
List<Student> StudentList = db.Students.ToList();
return View(StudentList);
}
public ActionResult Index(string option, string search)
{
//if a user choose the radio button option as Subject
if (option == "Subjects")
{
//Index action method will return a view with a student records based on what a user specify the value in textbox
return View(db.Students.Where(x => x.Subjects == search || search == null).ToList());
}
else if (option == "Gender")
{
return View(db.Students.Where(x => x.Gender == search || search == null).ToList());
}
else
{
return View(db.Students.Where(x => x.Name.StartsWith(search) || search == null).ToList());
}
}
}
}