Click here to Skip to main content
16,007,610 members
Articles / Web Development / ASP.NET
Tip/Trick

Priority Grid, an Application with ASP.NET MVC 4

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Mar 2013CPOL2 min read 15.2K   473   10   1
A solution for creating a priority grid
Image 1

Introduction

This tip contains a solution for creating a priority grid. There are scenarios when you use a grid in your application for a specific purpose. For example, you need a grid to increase or decrease the priority of the individual selected row. In that case, you don’t need to deploy a feature heavy grid. The priority grid, as the name indicates, should be sufficient for such limited purpose use. The application uses MVC / JSON with LINQ to handle the data access in the grid.

Background

The ASP.NET MVC Framework's Model View Controller pattern is increasingly growing in popularity for enabling users to build flexible, easily tested Web applications. A MVC application is also easy to maintain compared to web form application. Such an application would keep the main application code separate from persistence logic as well as from the front end view.

Using the Code

In our sample application, we use a repository (class) that performs the Linq query to retrieve the data from the input XML (note that we are using an XML file to populate the data in the grid). The MVC controller instantiates and calls the repository method to get the data. That way, any other controller that needs the same data can use the same repository without duplicating the code. We use separate classes within the model to pass data between controller and repository. To run the application, open the Visual Studio 2012 project and start. The application uses MVC ViewData; the ViewData (named ‘data’) is populated by LINQ call to the input XML and the returned ‘data’ is passed on to the front end jQuery html() method.

The increment priority method uses the following code:

C#
var item1 = data[ind];
var item2= data[ind-1];
data[ind-1]=item1;
data[ind]=item2;

The grid is refreshed after the current row is swapped with the previous as shown above. The Save method uses the code below:

C#
var myiD = Array();
    	$.each(data, function (index, value) {
        		myiD.push(value['id']);
         	 });

It then makes an AJAX request with jQuery and JSON to the MVC controller to update the priority (of the rows) in the XML.

Points of Interest

jQuery with LINQ has recently received a huge surge of interest of the ASP.NET MVC. I wanted to create an example where I would invoke an AJAX call to dynamically populate a grid with the additional functionality of increasing (or decreasing) the priority of the selected row. I've been impressed not only with how few lines of JavaScript it takes me to get things done, but also how (relatively) easy it was to learn. This application works for small or short-lived applications, but can also be extended for large applications.

History

  • 19th March, 2013: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am passionate on coding and application development specially in the .NET C# windows and web platform.

Comments and Discussions

 
Questiongrid Pin
Member 1497586926-Oct-20 12:36
Member 1497586926-Oct-20 12:36 
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Test Index html</title>



<!--styles-->

<!--<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">-->



<!--scripts-->

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

<!--<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>-->

</head>

<body>

<div class="container">

<div class="row" style="justify-content: center;">

<h1>Stock Option Table</h1>

</div>

<div class="row" id="stock"></div>

</div>

<script>

var jsonString = '{"StockOption": {"Description": "test", "Grant": [{"GrantDate": "2020-02-01", "VestDate": "2020-02-01", "ExpirationDate": "2020-02-01", "Symbol": "AFDD", "Type": "Qualified", "Quantity": "200000", "GrantPrice": "100"}, {"GrantDate": "2020-01-01", "VestDate": "2020-01-01", "ExpirationDate": "2020-01-01", "Symbol": "CSWQ", "Type": "Qualified", "Quantity": "300000", "GrantPrice": "200"}] } }';



var inputJSON = JSON.parse(jsonString);

var StockOption = inputJSON.StockOption;

var Grant = StockOption.Grant;



var random_id = function ()

{

var id_num = Math.random().toString(9).substr(2,3);

var id_str = Math.random().toString(36).substr(2);



return id_num + id_str;

}





//--->create data table > start

var tbl = '';

tbl +='<table class="table table-hover table-bordered">';



//--->create table header > start

tbl +='<thead>';

tbl +='<tr>';

tbl +='<th>Symbol</th>';

tbl +='<th>Type</th>';

tbl +='<th>Quantity</th>';

tbl +='<th>Grant Price</th>';

tbl +='<th>Grant Date</th>';

tbl +='<th>Vest Date</th>';

tbl +='<th>Expiration Date</th>';

tbl +='<th>Options</th>';

tbl +='</tr>';

tbl +='</thead>';

//--->create table header > end





//--->create table body > start

tbl +='<tbody>';



//--->create table body rows > start

$.each(Grant, function(index, val)

{

//you can replace with your database row id

var row_id = random_id();



//loop through ajax row data

tbl +='<tr row_id="'+row_id+'">';

tbl +='<td ><div class="row_data" edit_type="click" col_name="Symbol">'+val['Symbol']+'</div></td>';

tbl +='<td ><div class="row_data" edit_type="click" col_name="Type">'+val['Type']+'</div></td>';

tbl +='<td ><div class="row_data" edit_type="click" col_name="Quantity">'+val['Quantity']+'</div></td>';

tbl +='<td ><div class="row_data" edit_type="click" col_name="GrantPrice">'+val['GrantPrice']+'</div></td>';

tbl +='<td ><div class="row_data" edit_type="click" col_name="GrantDate">'+val['GrantDate']+'</div></td>';

tbl +='<td ><div class="row_data" edit_type="click" col_name="VestDate">'+val['VestDate']+'</div></td>';

tbl +='<td ><div class="row_data" edit_type="click" col_name="ExpirationDate">'+val['ExpirationDate']+'</div></td>';



//--->edit options > start

tbl +='<td>';



tbl +='<span class="btn_edit" > <a href="#" class="btn btn-link " row_id="'+row_id+'" > Edit</a> </span>';



//only show this button if edit button is clicked

tbl +='<span class="btn_save"> <a href="#" class="btn btn-link" row_id="'+row_id+'"> Save</a> | </span>';

tbl +='<span class="btn_cancel"> <a href="#" class="btn btn-link" row_id="'+row_id+'"> Cancel</a> | </span>';



tbl +='</td>';

//--->edit options > end



tbl +='</tr>';

});



//--->create table body rows > end



tbl +='</tbody>';

//--->create table body > end



tbl +='</table>';

//--->create data table > end



document.getElementById('stock').innerHTML = tbl;

$(document).find('.btn_save').hide();

$(document).find('.btn_cancel').hide();



$(document).ready(function () {

$(document).on('click', '.row_data', function(event)

{

event.preventDefault();



if($(this).attr('edit_type') == 'button')

{

return false;

}



//make div editable

$(this).closest('div').attr('contenteditable', 'true');

//add bg css

$(this).addClass('bg-warning').css('padding','5px');



$(this).focus();

});

$(document).on('click', '.btn_edit', function(event)

{

event.preventDefault();

var tbl_row = $(this).closest('tr');



var row_id = tbl_row.attr('row_id');



tbl_row.find('.btn_save').show();

tbl_row.find('.btn_cancel').show();



//hide edit button

tbl_row.find('.btn_edit').hide();



//make the whole row editable

tbl_row.find('.row_data')

.attr('contenteditable', 'true')

.attr('edit_type', 'button')

.addClass('bg-warning')

.css('padding','3px')



//--->add the original entry > start

tbl_row.find('.row_data').each(function(index, val)

{

//this will help in case user decided to click on cancel button

$(this).attr('original_entry', $(this).html());

});

//--->add the original entry > end



});

$(document).on('click', '.btn_cancel', function(event)

{

event.preventDefault();



var tbl_row = $(this).closest('tr');



var row_id = tbl_row.attr('row_id');



//hide save and cacel buttons

tbl_row.find('.btn_save').hide();

tbl_row.find('.btn_cancel').hide();



//show edit button

tbl_row.find('.btn_edit').show();



//make the whole row editable

tbl_row.find('.row_data')

.attr('edit_type', 'click')

.removeClass('bg-warning')

.css('padding','')



tbl_row.find('.row_data').each(function(index, val)

{

$(this).html( $(this).attr('original_entry') );

});

});

$(document).on('click', '.btn_save', function(event)

{

event.preventDefault();

var tbl_row = $(this).closest('tr');



var row_id = tbl_row.attr('row_id');





//hide save and cacel buttons

tbl_row.find('.btn_save').hide();

tbl_row.find('.btn_cancel').hide();



//show edit button

tbl_row.find('.btn_edit').show();





//make the whole row editable

tbl_row.find('.row_data')

.attr('edit_type', 'click')

.removeClass('bg-warning')

.css('padding','')



//--->get row data > start

var arr = {};

tbl_row.find('.row_data').each(function(index, val)

{

var col_name = $(this).attr('col_name');

var col_val = $(this).html();

arr[col_name] = col_val;

});

//--->get row data > end



//use the "arr" object for your ajax call

$.extend(arr, {row_id:row_id});



//out put to show

$('.post_msg').html( '<pre class="bg-success">'+JSON.stringify(arr, null, 2) +'</pre>')





});

//--->save single field data > start

$(document).on('focusout', '.row_data', function(event)

{

event.preventDefault();



if($(this).attr('edit_type') == 'button')

{

return false;

}



var row_id = $(this).closest('tr').attr('row_id');



var row_div = $(this)

.removeClass('bg-warning') //add bg css

.css('padding','')



var col_name = row_div.attr('col_name');

var col_val = row_div.html();



var arr = {};

arr[col_name] = col_val;



//use the "arr" object for your ajax call

$.extend(arr, {row_id:row_id});



//out put to show

$('.post_msg').html( '<pre class="bg-success">'+JSON.stringify(arr, null, 2) +'</pre>');



})

//--->save single field data > end

});

</script>



</body>

</html>

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.