Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
---------this is my Menu Model Class-------

public class Menu
{
public string LinkText { get; set; }
public string Actionname { get; set; }
public string routevalue{ get; set; }
public List<menu> menu { get; set; }

}

-----------------------Controller class-----------------
public ActionResult Index()
{
List<menu> mlist = new List<menu>();
Menu m = new Menu();
using (SqlConnection conn = new SqlConnection(Cstring))
{
conn.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT [TextLinkName],[ActionLinkName],[routevalue] FROM [MVCTESTING].[dbo].[tbl_MainMenu]", conn);
myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
m.LinkText = (string)myReader["TextLinkName"];
m.Actionname = (string)myReader["ActionLinkName"];
m.controllername = (string)myReader["routevalue"];
}

}
return View(mlist);
}

-------------------------I am stuck here how can we show in the Laout page------


What I have tried:

how we can show in the layout , action,controller and routevalue should be coming from database table
like this

Posted
Updated 30-Jan-19 23:36pm
v4

1 solution

Let me guess... your model is null.
The problem is that while you are populating the m object, it is never being added to the mlist object model which is being returned.

In the first couple of lines in the controller, you create 2 object instances
C#
List<menu> mlist = new List<menu>();
Menu m = new Menu();
And then you populate the m object by iterating through the SQL recordset
C#
while (myReader.Read())
{
m.LinkText = (string)myReader["TextLinkName"];
m.Actionname = (string)myReader["ActionLinkName"];
m.controllername = (string)myReader["routevalue"];
}
And finally you return your view
C#
return View(mlist);

Looks like the last line in your Reader.Read() loop should be something like
mlist.Add(m)
 
Share this answer
 
Comments
Member 10194266 31-Jan-19 1:04am    
error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[WebApplication1.Models.Menu]', but this dictionary requires a model item of type 'WebApplication1.Models.Menu'.
MadMyche 31-Jan-19 6:50am    
Shoulda been mlist.menu.add(), sorry.
MadMyche 4-Mar-20 10:39am    
This does not appear to be related to this; I would suggest you start this as a new question
Member 10194266 4-Mar-20 10:47am    
Thanks ,
I have create new ticket

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900