Waseem Bukhsh
Waseem Bukhsh

Reputation: 11

Render html control while using with lambda expression

I am using NonFactors Grid.Mvc (http://mvc-grid.azurewebsites.net/) and I found limitation in this Grid, if you see an example on http://mvc-grid.azurewebsites.net/Grid/SourceUrl

Code in above Example is(bold line contains the limitation that unable to pass lambda expression):

@model IEnumerable<Person>

 @(Html
.Grid(Model)
.Build(columns =>
{
    columns.Add(model => model.Name).Titled("Name");
    columns.Add(model => model.Surname).Titled("Surname");
    columns.Add(model => model.MaritalStatus).Titled("Marital status");

    columns.Add(model => model.Age).Titled("Age");
    columns.Add(model => model.Birthday).Titled("Birth date").Formatted("{0:d}");
    columns.Add(model => model.IsWorking).Titled("Employed");
})


.WithSourceUrl(Url.Action("SourceUrl", "Grid"))

.Empty("No data found")
.Pageable(pager =>
{
    pager.PagesToDisplay = 3;
    pager.RowsPerPage = 2;
})
.Filterable()
.Sortable()
)

I want replace the .WithSourceUrl(Url.Action("SourceUrl", "Grid")) with .WithSourceUrl(Url.Action("SourceUrl", "Grid", new {m => m.personId}))

and I did research to create the method like this

public MvcHtmlString WithSourceUrlFor<TValue>(Expression<Func<T, TValue>> expression)
    {

        var MvcHtmlString = ExpressionHelper.GetExpressionText(expression);

        return MvcHtmlString;

    }

but I was unable to convert lamda expression into MvcHtmlString and I am stuck.

Please help me.

Thanks

Upvotes: 1

Views: 580

Answers (1)

user10551572
user10551572

Reputation:

.WithSourceUrl is used to specify which url should the grid use to reload the content. You can't use person lambda in there, because what would the url be if you had one million people in the list.

I'm guessing that you want to create a link for each row, which can be achieved with

Html.Grid(Model).Build(columns =>
{
    columns
        .Add(model => "<a href='" + Url.Action("Edit", new { model.Id }) + "'>Edit</a>")
        .Encoded(false);
}

Upvotes: 1

Related Questions