Articles in this section
Category / Section

How to create Listview model in ASP.NET MVC?

2 mins read

You can refer to the following procedure to achieve this requirement.

 

Create Model

First, create model that contains texts field.

Model

public class ListData
    {
        public string texts { get; set; }
    }
    public static class ListDataModal {
        public static List<ListData> listTempSource = new List<ListData>();
        public static List<ListData> setListDataSource()
        {
            listTempSource.Add(new ListData { texts = "Discover Music" });
            listTempSource.Add(new ListData { texts = "Sales and Events" });
            listTempSource.Add(new ListData { texts = "Categories" });
            listTempSource.Add(new ListData { texts = "MP3 Albums" });
            listTempSource.Add(new ListData { texts = "More in Music" });
            return listTempSource;
        }
        public static void clearSource()
        {
            listTempSource.Clear();
        }
    }

 

Create Controller

Then, create controller and pass the datasource in the view.

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sample.Models; // refers to models
namespace Sample.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ListDataModal.clearSource(); //Avoids duplication of listview items from the model
            return View(ListDataModal.setListDataSource().ToList());
        }
    }
}

 

Create View

Create view page to add the codes for rendering the listview by using the datasource created earlier.

CSHTML

@model List<ListData>
@*Creates listview with datasource*@
@Html.EJMobile().ListView("locallistview").ShowHeader(true).HeaderTitle("ListView with DataSource").DataSource(Model).FieldSettings(f => { f.Text("texts"); })

 

Output

Listview

Figure 1: Listview


Conclusion

I hope you enjoyed learning how to create a Listview model in ASP.NET MVC.

You can refer to the ASP.NET MVC Listview feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Listview example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied