Wednesday, 8 April 2015

Hyperlink Adding In MVC WebGrid in Asp.Net MVC Using C#.Net

4/08/2015 - By Pranav Singh 2

This article will show you how you can add a hyperlink to open other website in a bind webgrid in asp.net mvc using c#.


So for this article first we will create the entity model.


Now we will create the model file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication1.Models
{
    public class ProductDetailModel
    {
        public List<Product> ProductList { get; set; }
    }
    public class Product
    {
        public int ID { get; set; }
        public string ProductName { get; set; }
        public string ProductDetail { get; set; }
        public int CurrentStock { get; set; }
    }
}

Now add the controller class file and the below code into the controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            ProductDetailModel objproductdetailmodel = new ProductDetailModel();
            objproductdetailmodel.ProductList = ProductData();
            return View(objproductdetailmodel);
        }
              ///
        /// This finction is used for providing the data.
        /// we will bind this data
        /// to diaply in grid format.
        /// You can get data from data base and
        /// thee put into collection
        ///  ///
        public List<Product> ProductData()
        {
            List<Product> objProduct = new List<Product>();
            DemoEntities objDemoEntities = new DemoEntities();
            var productItem = from data in objDemoEntities.ProductMasters
                              select data;
            foreach (var item in productItem)
            {
                objProduct.Add(new Product
                {
                    ID = item.Id,
                    ProductName = item.ProductName,
                    ProductDetail = item.ProductDetail,
                    CurrentStock = (int)item.CurrentStock
                });
            }
            return objProduct;
        }
    }
}

      

Now we will create the view and add the below code into the view

@model MvcApplication1.Models.ProductDetailModel
@{
    ViewBag.Title = "Hyperlink Adding In MVC WebGrid in Asp.Net MVC Using C#.Net";
}
<style>
    table, td, th
    {
        border: 1px solid green;
        border-collapse: collapse;
    }
   
    th
    {
        border: 1px solid black;
        background-color: green;
        color: white;
    }
</style>
<script language="javascript">
    function AlertMessage() {
        alert("This is a alert message");
    }

</script>
@using (@Html.BeginForm("Index", "Home"))
{
    var grid = new WebGrid(Model.ProductList, canSort: false);

    <div>
        @grid.GetHtml(columns:
                grid.Columns
                (
                        grid.Column("ID", "ID"),
                        grid.Column("ProductName", "Product Name"),
                        grid.Column("ProductDetail", "Product Detail"),
                        grid.Column("CurrentStock", "Current Stock"),
                          grid.Column("",format: @<a href="http://aspdotnet-pools.com">Click Me</a>
)
        ), mode: WebGridPagerModes.Numeric)
    </div>
  
}

In above code please check the below mention line

grid.Column("",format: @<a href="http://aspdotnet-pools.com">Click Me</a>)

This code is used for adding the hyper link. Now we have done run the application to check the output.



DOWNOLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. Good Post.....Thank you Pranav Singh.

    ReplyDelete
  2. Good Post.....Thank you Pranav Singh.

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top