In this article, I will show how to perform random sort list c# asp.net.The .Net 3.5 framework has Linq,it is a very good feature, it allows us to perform complex operations with efficiency .
Step 1: Create an asp.net mvc project and right click on the controller folder and create a new controller and name it as HomeController. Inside the HomeController copy and paste the following code.
public class RandomSortController : Controller
{
//
// GET:/RandomSort/
models db = new models();
public ActionResult Index()
{
return View(db.Departments.OrderBy(emp => Guid.NewGuid()).ToList());
}
}
Step 2: Right click on the Share folder and create a razor view named as index. Copy and paste the following code.
@modelIEnumerable<MVC_tutorials.Models.Department>
<h2>Random Sort a List Using LINQ</h2>
<p>
@Html.ActionLink("CreateNew", "AddDepartment")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.DepartmentID)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DepartmentID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink("Edit", "EditDepartmentDetails", new { id = item.DepartmentID }) |
@Html.ActionLink("Delete", "DeleteDepartment", new { id = item.DepartmentID })
</td>
</tr>
} </table>
Description: Run the application, the department object list get randomly sorted and displayed on the page.
Post your comments / questions
Recent Article
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
Related Article