This article
will show you how you can use actionlink in a webgrid control in mvc
application using c#.net. This article we can use in MVC3, MVC4, MVC5, MVC6.
Have a look
of this article also for hyperlink: Hyperlink
Adding In MVC WebGrid in Asp.Net MVC Using C#.Net, How
to Bind Data to Webgrid in ASP.net MVC Using C#.Net, Add
Textbox In WebGrid and Access TextBox Value In Controller In Asp.net MVC Using
C#.Net, Search
and Display Data In MVC WebGrid in Asp.Net MVC Using C#.Net.
Now for
this article first we will create a sql table and add some record.
After creating table
we will create a mvc application. After creating mvc application we will create
model.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
MvcApplication9.Models
{
public class StudentDetails
{
public List<StudentDetail>
Studentmodel { get; set; }
}
public class StudentDetail
{
public int Id { get; set; }
public string Name { get; set; }
public string Class { get; set; }
public string Section { get; set; }
public string Address { get; set; }
}
}
|
After creating model
add the table in your entity file(.edmx)
Now we will come to
our controller end and add the below code in your controller.
using
MvcApplication9.Models;
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace
MvcApplication9.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult
Index()
{
StudentDetails
_objuserloginmodel = new StudentDetails();
List<StudentDetail>
_objStudent = new List<StudentDetail>();
_objStudent = GetStudentList();
_objuserloginmodel.Studentmodel = _objStudent;
return
View(_objuserloginmodel);
}
public List<StudentDetail>
GetStudentList()
{
List<StudentDetail>
objStudent = new List<StudentDetail>();
/*Create instance of entity model*/
NorthwindEntities
objentity = new NorthwindEntities();
/*Getting data from database for user validation*/
var _objuserdetail = (from data in objentity.Students
select data);
foreach (var item in _objuserdetail)
{
objStudent.Add(new StudentDetail { Id = item.Id, Name = item.Name, Section =
item.Section, Class = item.Class, Address = item.Address });
}
return objStudent;
}
}
}
|
In above code I have
first created instance for the entity class. After that we have used linq query
to access the value from table. After that I have assign it to model. Now we
have done at controller end. We will create view and add the below code in our
view.
@model MvcApplication9.Models.StudentDetails
@{
ViewBag.Title
= "Asp.Net MVC: WebGrid With
ActionLink In MVC6 Using C#.Net";
}
<style>
table, td, th {
border: 1px solid green;
border-collapse: collapse;
}
th {
border: 1px solid black;
background-color: green;
color: white;
}
</style>
@using (@Html.BeginForm("Index", "Home"))
{
var grid = new WebGrid(Model.Studentmodel,
canSort: false);
<div>
@grid.GetHtml(columns:
grid.Columns
(
grid.Column("ID", "Stud ID"),
grid.Column("Class", "Class"),
grid.Column("Section", "Section"),
grid.Column("Address", "Address"),
grid.Column(columnName: "Student
Detail", format: (item) => Html.ActionLink(((string)item.Name), "Detail", new { id = item.ID
}))),
mode: WebGridPagerModes.Numeric)
</div>
}
|
Now check the above highlighted
part of code. This piece of code we will use for showing link on the name of
the student. Now we have done run the application to check the output.
0 comments:
Please let me know your view