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.
Some of my precious articles are as follows: MVC
Pass Multiple Parameters in QueryString or URL in Asp.Net MVC, Search
and Display Data In MVC WebGrid in Asp.Net MVC Using C#.Net, Hyperlink
Adding In MVC WebGrid in Asp.Net MVC Using C#.Net, Bind
WebGrid With Entity Framework in Asp.Net MVC Using C#.Net, jQuery
Magnifying Glass Zoom Plugin Effect Css in Asp.net MVC, Strong
Random String Password Generation Using C#.Net.
So for this article first we will create a new asp.net mvc application and add model class file in it. In this model class file we will add the below code.
So for this article first we will create a new asp.net mvc application and add model class file in it. In this model class file we will add the below code.
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.
0 comments:
Please let me know your view