如何在ASP.NET MVC中显示动态类别菜单和子类别菜单 [英] How to display dynamic categories menu and subcategories menu in ASP.NET MVC
本文介绍了如何在ASP.NET MVC中显示动态类别菜单和子类别菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是初学者,我希望在中间显示菜单,例如本网站(www.kammadanam.com),当用户点击我要显示的任何类别名称或其子类别名称时与点击类别相关的产品。
i我正在尝试这种方式,但它不正确请提供有效代码
i在我的项目中被打了一周之前。
请帮帮我朋友
我的尝试:
我的表:
< pre>类别:
CategoryId(pk),CategoryName,Description,
子类别:
SubCategoryId( pk),SubCategoryName,Description,Icon,CategoryId(fk)
产品:
ProductId(pk),名称,价格,描述,CategoryId(fk),SubCategoryId(fk ),
我的控制器是:
public ActionResult Home()
{
Products obj = new Products();
// =====菜单Menthods =====
obj.Showall_Menu_Cate = obj.selectalldata_Menu_Cate();
return View(obj);
}
public JsonResult GetSiteMenu(string id)
{
Sampleobj = new Sample();
DataTable dt = new DataTable();
列表<样本> PrtList = null;
SqlCommand Cmd = new SqlCommand(select * from SubCategory,其中CategoryID ='+ id +',Con);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
DataSet Ds = new DataSet();
Da.Fill(Ds);
PrtList = new List< Sample>();
for(int i = 0; i< Ds.Tables [0] .Rows.Count; i ++)
{
Sample CatObj = new Sample();
//CatObj.Id = Ds.Tables [0] .Rows [i] [Id]。ToString();
CatObj.SubCategoryID = Ds.Tables [0] .Rows [i] [CategoryID]。ToString();
CatObj.SubCategoryName = Ds.Tables [0] .Rows [i] [CategoryName]。ToString();
//CatObj.Description = Ds.Tables [0] .Rows [i] [Description]。ToString();
//CatObj.Image = Ds.Tables [0] .Rows [i] [Image]。ToString();
//CatObj.CreatedOn = Ds.Tables [0] .Rows [i] [CreatedOn]。ToString();
//CatObj.Status = Ds.Tables [0] .Rows [i] [Status]。ToString();
PrtList.Add(CatObj);
}
//Productobj.Showall_Menu_Category = PrtList;
返回新的JsonResult {Data = PrtList};
}
我的模特是:
public List< Sample> selectalldata_Menu_Cate()
{
List< Sample> PrtList = null;
SqlCommand Cmd = new SqlCommand(select * from Category,Con);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
DataSet Ds = new DataSet();
Da.Fill(Ds);
PrtList = new List< Sample>();
for(int i = 0; i< Ds.Tables [0] .Rows.Count; i ++)
{
Sample CatObj = new Sample();
CatObj.Id = Ds.Tables [0] .Rows [i] [Id]。ToString();
CatObj.CategoryID = Ds.Tables [0] .Rows [i] [CategoryID]。ToString();
CatObj.CategoryName = Ds.Tables [0] .Rows [i] [CategoryName]。ToString();
CatObj.Description = Ds.Tables [0] .Rows [i] [Description]。ToString();
CatObj.Image = Ds.Tables [0] .Rows [i] [Image]。ToString();
CatObj.CreatedOn = Ds.Tables [0] .Rows [i] [CreatedOn]。ToString();
CatObj.Status = Ds.Tables [0] .Rows [i] [Status]。ToString();
PrtList.Add(CatObj);
}
返回PrtList;
}
查看:
@foreach(var item in Model.Showall_Menu_Products)
{
< li class =dropdown>
< a href =@ Html.ActionLink(item.ProductName,Shop,Kammadanam,new {id =Pandu2},new {@ class =.button_action})> < / A>
< ul>
@foreach(Model.Showall_Menu_Category中的var item2)
{
< li>
@ * @ Html.ActionLink(太阳能热水器,商店,Kammadanam)* @
< a href =@ Html.ActionLink(item2.CategoryName,Shop, Kammadanam,新{id =Pandu},新{@ class =.button_a})>< / a>
< / li>
}
@ *< li>< a href =shop.html>太阳能LED灯< / a>< / li>
< li>< a href =shop.html>太阳能水泵< / a>< / li>
< li>< a href =shop.html> solar potable led&灯LED< / a>< / li> * @
< / ul>
< / li>
}
Ajax方法:
<脚本>
$('。button_action')。click(函数(索引){
$ .ajax({
url:/ Kammadanam / getProduct /,
contentType:'application / html; charset = utf-8',
类型:'GET',
数据:{
id:$(#Pandu2)。val()
},
成功:函数(结果){
调试器
$(#Pandu)。attr('href',结果)
}
});
返回false;
});
< / script>
解决方案
('。button_action')。click(function(index){
.ajax({
url:/ Kammadanam / getProduct /,
contentType:'application / html; charset = utf-8',
类型: 'GET',
数据:{
id:
(#Pandu2)。val()
},
成功: function(result){
debugger
I'm a beginner and I want to display menu on the middle like This Website(www.kammadanam.com) and when user clicks on any Category Name or its Sub Category Name I want to display Products related to the clicked category.
i am trying this way but it is not correct please provide valid code
i am struck in my project before 1 week.
please help me friends
What I have tried:
MY TABLE:
<pre>Category: CategoryId(pk), CategoryName, Description, SubCategory: SubCategoryId(pk), SubCategoryName, Description, Icon,CategoryId(fk) Product: ProductId(pk), Name, Price, Description, CategoryId(fk), SubCategoryId(fk),
my controller is:
public ActionResult Home() { Products obj = new Products(); //=====Menu Menthods===== obj.Showall_Menu_Cate = obj.selectalldata_Menu_Cate(); return View(obj); }
public JsonResult GetSiteMenu(string id) { Sample Productobj = new Sample(); DataTable dt = new DataTable(); List<Sample> PrtList = null; SqlCommand Cmd = new SqlCommand("select * from SubCategory where CategoryID='" + id + "'", Con); SqlDataAdapter Da = new SqlDataAdapter(Cmd); DataSet Ds = new DataSet(); Da.Fill(Ds); PrtList = new List<Sample>(); for (int i = 0; i < Ds.Tables[0].Rows.Count; i++) { Sample CatObj = new Sample(); //CatObj.Id = Ds.Tables[0].Rows[i]["Id"].ToString(); CatObj.SubCategoryID = Ds.Tables[0].Rows[i]["CategoryID"].ToString(); CatObj.SubCategoryName = Ds.Tables[0].Rows[i]["CategoryName"].ToString(); //CatObj.Description = Ds.Tables[0].Rows[i]["Description"].ToString(); //CatObj.Image = Ds.Tables[0].Rows[i]["Image"].ToString(); //CatObj.CreatedOn = Ds.Tables[0].Rows[i]["CreatedOn"].ToString(); //CatObj.Status = Ds.Tables[0].Rows[i]["Status"].ToString(); PrtList.Add(CatObj); } //Productobj.Showall_Menu_Category = PrtList; return new JsonResult { Data = PrtList }; }
my Model is:
public List<Sample> selectalldata_Menu_Cate() { List<Sample> PrtList = null; SqlCommand Cmd = new SqlCommand("select * from Category", Con); SqlDataAdapter Da = new SqlDataAdapter(Cmd); DataSet Ds = new DataSet(); Da.Fill(Ds); PrtList = new List<Sample>(); for (int i = 0; i < Ds.Tables[0].Rows.Count; i++) { Sample CatObj = new Sample(); CatObj.Id = Ds.Tables[0].Rows[i]["Id"].ToString(); CatObj.CategoryID = Ds.Tables[0].Rows[i]["CategoryID"].ToString(); CatObj.CategoryName= Ds.Tables[0].Rows[i]["CategoryName"].ToString(); CatObj.Description = Ds.Tables[0].Rows[i]["Description"].ToString(); CatObj.Image = Ds.Tables[0].Rows[i]["Image"].ToString(); CatObj.CreatedOn = Ds.Tables[0].Rows[i]["CreatedOn"].ToString(); CatObj.Status = Ds.Tables[0].Rows[i]["Status"].ToString(); PrtList.Add(CatObj); } return PrtList; }
View:
@foreach (var item in Model.Showall_Menu_Products) { <li class="dropdown"> <a href="@Html.ActionLink(item.ProductName,"Shop", "Kammadanam",new { id="Pandu2" },new { @class= ".button_action"})"></a> <ul> @foreach (var item2 in Model.Showall_Menu_Category) { <li> @*@Html.ActionLink("Solar Water heaters", "Shop", "Kammadanam")*@ <a href="@Html.ActionLink(item2.CategoryName,"Shop", "Kammadanam",new { id ="Pandu" },new { @class= ".button_a"})"></a> </li> } @*<li><a href="shop.html">Solar LED Lights</a></li> <li><a href="shop.html">solar water pumps</a></li> <li><a href="shop.html">solar potable led & lamp LED</a></li>*@ </ul> </li> }
Ajax Method:
<script> $('.button_action').click(function (index) { $.ajax({ url: "/Kammadanam/getProduct/", contentType: 'application/html; charset=utf-8', type: 'GET', data: { id: $("#Pandu2").val() }, success: function (result) { debugger $("#Pandu").attr('href', result) } }); return false; }); </script>
解决方案
('.button_action').click(function (index) {
.ajax({ url: "/Kammadanam/getProduct/", contentType: 'application/html; charset=utf-8', type: 'GET', data: { id:
("#Pandu2").val() }, success: function (result) { debugger
这篇关于如何在ASP.NET MVC中显示动态类别菜单和子类别菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文