Strongly typed HTML helper display

Current using viewdata to display data in grid, I hope to retrieve data using Strong Type HTML Helper and display in using model in .cshtml
      

CSHTML Code:
@using Syncfusion.EJ2
@model IEnumerable<TestData>
@using ViewModel

<div> <p style="font-weight: bold;">Table View  </p></div>

@{
    List<object> cols = new List<object>();
    cols.Add(new { field = "OrderDate", direction = "Ascending" });
    cols.Add(new { field = "Freight", direction = "Descending" });
}
@{
    List<object> toolbarItems = new List<object>();
    toolbarItems.Add(new { text = "Create", tooltipText = "Create", prefixIcon = "e-expand", id = "Create" });
    toolbarItems.Add(new { text = "Search", tooltipText = "Search", prefixIcon = "e-expand", id = "Search" });
}
<div class="control-section">
    @Html.EJS().Grid("GridView").DataSource((IEnumerable<object>)ViewBag.dataSource).Height("100%").Width("auto").AllowSorting().AllowMultiSorting(
    ).AllowFiltering(true).AllowPaging().AllowGrouping().AllowSelection().ToolbarClick("toolbarClick").FilterSettings(<lambda expression>).Columns(col =>
    {
        col.Field("DocumentStatus").HeaderText("Document Status").Type("string").Width("160").Add();
        col.Field("DocumentNo").HeaderText("Document No").Type("string").Width("170").Add();
        col.Field("DocumentType").HeaderText("Document Type").Type("string").Width("170").Add();
        col.Field("DocumentOwner").HeaderText("Document Owner").Type("string").Width("170").Add();
        col.Field("DocumentTitle").HeaderText("Document Title ").Type("string").Width("140").Add();
        col.Field("Progression").HeaderText("Progression").Type("double").Width("140").Add();
        col.Field("PlanStatus").HeaderText("Plan Status").Type("string").Width("170").Type("string").Add();
        col.Field("DeploymentDate").HeaderText("Deployment Date").Width("170").Type("date").Format("dd/MM/yyyy").Add();
        col.Field("Revision").HeaderText("Revision").Width("170").Type("string").Add();
        col.Field("ProgressStage").HeaderText("Progress Stage").Width("170").Type("string").Add();

    }).Toolbar(toolbarItems).PageSettings(page => page.PageCount(5)).Render()
</div>


1 Reply

MS Magesh Sabapathi Syncfusion Team March 4, 2020 09:04 AM UTC

Hi Kong, 

Greetings from syncfusion support 

Query : Use Model in .cshtml instead of viewdata to display Grid in MVC. 

Based on your query we suspect that you need to display the data in Grid using model. We have created an sample and in that sample the datas are displayed in the Grid as Model binding instead of Viewdata.  

Please refer that sample and code snippet. 

Index.cshtml 

@model WebApplication42.Models.SalesOrders 

@{ 
    List<object> toolbarItems = new List<object>(); 
    toolbarItems.Add(new { text = "Create", tooltipText = "Create", id = "Create" }); 
    toolbarItems.Add(new { text = "Search", tooltipText = "Search", id = "Search" }); 

@Html.EJS().Grid("EditParam").DataSource(Model.OrderItems).AllowFiltering(true).AllowPaging().AllowGrouping().AllowSelection().Columns(col => 
    col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("120").ValidationRules(new { required = "true" }).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("CustomerID").HeaderText("Customer Name").Width("150").ValidationRules(new { required = "true", minLength = 3 }).Add(); 
     

    }).AllowPaging().Toolbar(toolbarItems).PageSettings(page => page.PageCount(5)).Render() 




Please get back to us if you need further assistance 

Regards 
Magesh 


Loader.
Up arrow icon