I am using the grid source code from this website:
http://gridmvc.azurewebsites.net/
but I don't understand how the query string is formed for his custom columns. On the link I have provided there is an edit column, if you hover over the link you can see the hyperlink with the query string ID. I have used this source code but there is no query string for me.
Controller code
#region PartialViews
[ChildActionOnly]
public ActionResult RenderGrid()
{
DataLayer.RepositoryClient RC = new RepositoryClient();
var People = RC.GetPeople();
return PartialView("~/Views/Client/PartialViews/P_PersonGrid.cshtml", People);
}
#endregion
I get a list of people from entity framework. Then I use a list of models in my partial view.
Partial view
@model List<Models.modelPeopleGrid>
@using GridMvc.Html
@{
ViewBag.Title = "Person records";
}
<div>
@Html.Label("Search for person")
<input type="text" onkeyup="activateSearch()" id="txtSearch" style="width:150px";/>
</div>
<div class="grid-wrap" style="float:left">
@Html.Grid(Model).Named("peopleGrid").Columns(columns =>
{
columns.Add(m => m.Firstname).Titled("Firstname").Filterable(true);
columns.Add(m => m.Surname).Titled("Surname").Filterable(true);
columns.Add(m => m.DateOfBirth).Titled("DateOfBirth").Filterable(true);
columns.Add(m => m.Address).Titled("Address").Filterable(true);
columns.Add(m => m.Borough).Titled("Borough").Filterable(true);
columns.Add(m => m.PersonID).Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(
@<b>
@Html.ActionLink("Edit person details", "EditPerson", "Index")
</b>);
columns.Add()
.Encoded(false)
.Sanitized(false)
.SetWidth(30)
.RenderValueAs(m =>
@<b>
@Html.ActionLink("Add/remove claims", "EditClaim", "Index")
</b>);
}).WithPaging(20)
</div>
<div class="rowValues" style="float:right"></div>
<script>
$(function () {
pageGrids.peopleGrid.onRowSelect(function (e) {
$(".rowValues").html(
"<h4>Full details of person:</h4>" +
"Firstname: " + e.row.Firstname + "<br />" +
"Surname: " + e.row.Surname + "<br />" +
"DateOfBirth: " + e.row.DateOfBirth + "<br />" +
"Address: " + e.row.Address + "<br />" +
"Borough: " + e.row.Borough + "<br />"
);
});
});
</script>
<script type="text/javascript">
function activateSearch() {
var value = $("#txtSearch").val();
$.ajax({
url: "Client/GetPeople",
dataType: 'html',
type: "GET",
data: { Name: value },
success: function (data) {
$('.webgrid').empty().html(data);
},
error: function () {
alert("error");
}
});
}
</script>
I don't know how to get a query string formed for this line of code:
columns.Add(m => m.PersonID).Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(
@<b>
@Html.ActionLink("Edit person details", "EditPerson", "Index")
</b>);
I tried html object attributes as the fourth parameter of actionlink but I was getting a red swiggly for some reason.
Anyone know how to answer my problem?
As seen in the example on the website you've linked to, the RenderValueAs
method takes a lambda expression where the parameter represents the current model for the row.
I tried html object attributes as the fourth parameter of actionlink but I was getting a red swiggly for some reason.
Usually Razor Intellisense and syntax highlighting in Visual Studio is very buggy. Never trust it. The fact that you are getting red squiggles in a Razor page doesn't mean that you have an error in your syntax. In most cases it means that it is the syntax highlighter that simply doesn't understand the syntax. The only reliable thing is to run your application and see if it works.
So with this in mind you may try:
columns
.Add(m => m.PersonID)
.Encoded(false)
.Sanitized(false)
.SetWidth(30)
.RenderValueAs(m =>
@<b>
@Html.ActionLink(
linkText: "Edit person details",
actionName: "EditPerson",
controllerName: "Index", // <-- Huh, Index looks like an action name, double check that
routeValues: new { id = m.PersonID },
htmlAttributes: null
)
</b>
);
Notice how I have used explicit parameter names to make the code more readable and avoid any ambiguities with the gazzillions of overloads of the ActionLink method that the designers of the framework provided us.