Reset (Back to full list) in ASP.Net MVC

Steve henry 1 Reputation point
2022-04-30T06:23:56.533+00:00

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());  
        }  
    }  

}  

}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,678 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,541 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AgaveJoe 28,861 Reputation points
    2022-04-30T12:45:03.007+00:00

    The logic seems overly complex and I'm pretty sure you do not need all the conditional statements but the main point is LINQ allows for creating a dynamic query. Use the conditional logic to create the query rather than returning a View with hard coded queries.

    Programming pattern. Keep in mind, this code is untested but you should get the idea.

     public ActionResult Index(string option, string search)  
     {  
         var query = db.Students;  
           
         //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    
             query = query.Where(x => x.Subjects == search || search == null);  
         }  
         else if (option == "Gender")  
         {  
             query = query.Where(x => x.Gender == search || search == null);  
         }  
         else  
         {  
             query = query.Where(x => x.Name.StartsWith(search) || search == null));  
         }  
           
         return View(query.ToList());  
     }  
    

    Also, this type of pattern is covered in MVC tutorials. I recommend the following.

    Getting started with ASP.NET MVC 5
    Getting Started with Entity Framework 6 Code First using MVC 5

    0 comments No comments

  2. Bruce (SqlWork.com) 68,311 Reputation points
    2022-04-30T15:23:46.903+00:00

    The reset action is displaying the reset view, what does it look like?

    You should change reset to be named index, and use attributes [HttpGet] and [HttpPost] to distinguish the two actions. Then change the reset button to a link, rather than form post.

    0 comments No comments

  3. Yijing Sun-MSFT 7,086 Reputation points
    2022-05-02T03:34:58.26+00:00

    Hi @Steve henry ,
    You don't call the Reset method. You submit the form with the Index method. The reset action is same like refresh the page if you bind the data before.You could return RedirectToAction.
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.