Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with displaying columns selected from a table in ASP.NET MVC project

Hello,

I would like to display columns from a table for only the ones selected in my business logic, how do I modify my code to achieve this? and also how do I search the table by entering data in a textbox?

1, Home Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AOP6TEST.Models;
using static DataLibrary.BusinessLogic.AOP6Processor;

namespace AOP6TEST.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ViewAOP6()
        {
            ViewBag.Message = "AOP-6 Data";

            var data = LoadAOP6();
            List<AOP6Model> AOP6 = new List<AOP6Model>();

            foreach (var row in data)
            {
                AOP6.Add(new AOP6Model
                {
                    SN = row.SN,
                    AGD1 = row.AGD1,
                    COUNTRY = row.COUNTRY,
                });
            }

            return View(AOP6);
        }
    }
}

2, Business Logic
using DataLibrary.DataAccess;
using DataLibrary.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataLibrary.BusinessLogic
{
    public static class AOP6Processor
    {
        public static List<AOP6Model> LoadAOP6()
        {
            string sql = @"select SN, AGD1, COUNTRY
                           from dbo.AOP6;";

            return SqlDataAccess.LoadData<AOP6Model>(sql);
        }
    }
}

Data Access Layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DataLibrary.DataAccess
{
    public static class SqlDataAccess
    {
        public static string GetConnectionString(string connectionName = "MVCDemoDB")
        {
            return ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
        }

        public static List<T> LoadData<T>(string sql)
        {
            using (IDbConnection cnn = new SqlConnection(GetConnectionString()))
            {
                return cnn.Query<T>(sql).ToList();
            }
        }

        public static int SaveData<T>(string sql, T data)
        {
            using (IDbConnection cnn = new SqlConnection(GetConnectionString()))
            {
                return cnn.Execute(sql, data);
            }
        }
    }
}
Avatar of Camillia
Camillia
Flag of United States of America image

how do I search the table by entering data in a textbox

You will have textbox with a button next to it. Call it Search. User enters something in the textbox and clicks "Search". You can have a stored procedure that passes in the text entered by the user. In your stored procedure, you will select from the table where whatever column is like the text entered by the user. You return the data and bind to your page.
Avatar of Victor  Charles

ASKER

Hi,

Thanks for your input, I was looking for assistance on how to include those controls and bind the webgrid to my table  in an MVC project. Can you provide furrher guidance on how to achieve this in an MVC project?
Thanks,
V.
Google for examples. It's hard to code it for you without spending time on your code. You'll learn more if you do it yourself first.
Agree. Thanks.
Hi,

Can you please help with missing reference, using the code below, all seems well except for error in line:

@grid.PagerList(mode: WebGridPagerModes.All, paginationStyle: "pagination pagination-small pagination-right")

PagerList is not recognized, apparently a reference is missing.

Code:

@model List <WebGridTest.DataTesting>

@{
    Layout = null;
    var grid = new WebGrid(canPage: true, rowsPerPage: 10);
    grid.Bind(source: Model, rowCount: ViewBag.TotalRows, autoSortAndPage: false);
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <h2>Employees List</h2>
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a href="#" class="navbar-brand">Search</a>
                </div>
                <div class="collapse navbar-collapse">
                    @using (Html.BeginForm("index", "home", FormMethod.Get, new { @class = "navbar-form navbar-left" }))
                    {
                        <div class="form-group">
                            <input type="text" name="search" value="@ViewBag.search" class="form-control" placeholder="search" />
                        </div>
                        <button type="submit" value="Search" class="btn btn-success">Search</button>
                    }
                </div>
            </div>
        </nav>
        <div>
            @grid.Table(
                tableStyle: "table table-responsive table-bordered",
                columns: grid.Columns(
                    grid.Column(columnName: "FirstName", header: "First Name"),
                    grid.Column(columnName: "LastName", header: "Last Name")
                )
            )

            <div class="row">
                <div class="col-sm-6">
                    @grid.PagerList(mode: WebGridPagerModes.All, paginationStyle: "pagination pagination-small pagination-right")
                </div>
            </div>

            @Html.Hidden("dir", grid.SortDirection)
            @Html.Hidden("col", grid.SortColumn)

        </div>
    </div>
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>

    <script>
        $(function () {
            try {
                var dir = $('#dir').val();
                var col = $('#col').val();
                var header = $("th a[href*=" + col + "]");
                if (dir == "Ascending") {
                    header.text(header.text() + "  ▲")
                }
                if (dir == "Descending") {
                    header.text(header.text() + "  ▼");
                }

            } catch (e) {

            }
        })
    </script>
</body>
</html>

Thanks,

Victor
You're using webgrid? My guess is PagerList is not part of whatever version of webgrid you're using OR you need to add the reference or library.

Side note: wrap your code in "code" tag. Hard to read your code.
Hello again,

I received the following error: 'WebGrid' does not contain a definition for 'PagerList' and no accessible extension method 'PageList' accepting a first argument of type 'WebGrid' could be found (are you missing a using directive or an assembly?)

The only fdifference in my project from the sample project I got the code is I am using <script src="~/Scripts/jquery-3.4.1.js"></script> instead of  <script src="~/Scripts/jquery-3.3.1.js"></script>.

Kind regards,

Victor
Thank you, it was helpful but still no solution, will keep looking.
You said you're following an example, correct? what is that example? can you post the link?
Hi,

I wrote the code following a training video videotraining video. I think the issue is I wss not able to add s .cs file as shown on the video.

Victor
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.