Wednesday, 24 June 2015

Bind XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using C#.Net

6/24/2015 - By Pranav Singh 0

This article I will show you how you can bind xml file data to gridview by category and  subcategory in asp.net mvc using c#.net.


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

namespace MvcApplication2.Models
{
    public class DataDisplayModel
    {
        public List<MainCategory> MainCategoryModel { get; set; }
    }
    public class MainCategory
    {

        public string MainCat { get; set; }
        public int Id { get; set; }
        public List<SubCategory> SubCategoryModel { get; set; }
    }
    public class SubCategory
    {
        public string SubCat { get; set; }
        public int Id { get; set; }
    }
}

Now we will add the XML class file. This XML class file data we will read and display in the form of category and subcatgory.

<?xml version="1.0" encoding="utf-8" ?>
<MainItem>
  <Category>
    <MainCategory>Color</MainCategory>
    <Category Name="Green" Id="1"></Category>
    <Category Name="Blue" Id="2"></Category>
    <Category Name="Red" Id="3"></Category>
  </Category>
  <Category>
    <MainCategory>Language</MainCategory>
    <Category Name="English" Id="4"></Category>
    <Category Name="Hindi" Id="5"></Category>
  </Category>
</MainItem>

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
using System.Data;

namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            DataDisplayModel objdatadisplaymode = new DataDisplayModel();
            objdatadisplaymode = GetCategoryList();
            return View(objdatadisplaymode);
        }
        [HttpPost]
        public ActionResult Index(int id = 0)
        {
            ViewBag.SelectedItem = Request.Form["SubCatName"].ToString();
            DataDisplayModel objdatadisplaymode = new DataDisplayModel();
            objdatadisplaymode = GetCategoryList();
            return View(objdatadisplaymode);
        }
        public DataDisplayModel GetCategoryList()
        {
            DataDisplayModel objdatadisplaymode = new DataDisplayModel();
            objdatadisplaymode.MainCategoryModel = new List<MainCategory>();
            //Load xml data into dataset
            DataSet objds = new DataSet();
            objds.ReadXml(Server.MapPath("~\\App_Data\\XMLFile1.xml"));
            if (objds.Tables[0].Rows.Count > 0)
            {
                DataTable objdtmaincat = new DataTable();
                objdtmaincat = objds.Tables[0].AsEnumerable().Where(m => m.Field<string>("MainCategory") != null).CopyToDataTable();
                for (int i = 0; i < objdtmaincat.Rows.Count; i++)
                {
                    MainCategory objmaincategory = new MainCategory();
                    objmaincategory.MainCat = objdtmaincat.Rows[i]["MainCategory"].ToString();
                    objmaincategory.Id = Convert.ToInt32(objdtmaincat.Rows[i]["Category_Id"].ToString());

                    //Get Sub categggory from the main collection
                    objmaincategory.SubCategoryModel = new List<SubCategory>();
                    DataTable objdtSubCat = new DataTable();
                    objdtSubCat = objds.Tables[0].AsEnumerable().Where(m => m.Field<string>("MainCategory") == null && m.Field<int>("Category_Id_0") == Convert.ToInt32(objdtmaincat.Rows[i]["Category_Id"].ToString())).CopyToDataTable();
                    for (int j = 0; j < objdtSubCat.Rows.Count; j++)
                    {

                        objmaincategory.SubCategoryModel.Add(new SubCategory { Id = Convert.ToInt32(objdtSubCat.Rows[j]["Id"].ToString()), SubCat = objdtSubCat.Rows[j]["Name"].ToString() });
                    }
                    objdatadisplaymode.MainCategoryModel.Add(objmaincategory);
                }
            }
            return objdatadisplaymode;
        }
    }
}

In this I have added get and post function into the class file. In above code I have read the ml file and then populated data into the model class file. Here is the data which we will read from XML file.



Now we will create the view for the controller action.

@model MvcApplication2.Models.DataDisplayMode
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "Home"))
{
    <h3>
        Bind XML File Data to Gridview By Category and<br />
        SubCategory in Asp.Net MVC Using C#.Net</h3>
 
    for (int i = 0; i < Model.MainCategoryModel.Count; i++)
    {
    <div>
        <b>
            @Model.MainCategoryModel[i].MainCat
        </b>
    </div>
    <div>
        <table>
            <tr>
                @for (int j = 0; j < Model.MainCategoryModel[i].SubCategoryModel.Count; j++)
                {
                    <td>
                        <input type="checkbox" name="SubCatName" value="@Model.MainCategoryModel[i].SubCategoryModel[j].Id"/>
                        @Model.MainCategoryModel[i].SubCategoryModel[j].SubCat
                    </td>
                }
            </tr>
        </table>
    </div>
  
    }
    <br />
    <div>
        <input type="submit" value="Submit" />
    </div>
    <div>
        Selected Item Id : @ViewBag.SelectedItem
    </div>
}

Here in controller I have used request.form to read the selected checkbox value. Now we have done run the application to check the output. Now as the break point will hit we will get the selected checkbox value.



Now here is the final output.


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

0 comments:

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