1
Answer

Getting error while loading values from database and display

Rohit Singh

Rohit Singh

7y
295
1
Hello everyone, I want to read all the values from my table and display it on my view using code first approach of entity framework but i got an error in every try.

I have tried almost everything but couldn't achieve what I want..please solve this problem.. Review my Code Below:::

What I have tried:

This is My ActionMethod: in which i'm reading all the values from databsae.

[HttpGet]
public ActionResult ViewAll()
{
MenuRolesSample dbContext = new MenuRolesSample();
return View(dbContext.Menus.ToList());

}

This is my Viewpage: where I just want to display all the Names from the database table.

@model IEnumerable

@{
ViewBag.Title = "ViewAll";
Layout = "~/Views/sahred/_layout.cshtml";
}
@foreach (var item in Model)
{
}


Name
@Html.DisplayFor(M=>item.Name)

Name

@Html.DisplayFor(M=>item.Name)



The Error which I've been facing is:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MenuRoles.Models.Menu]', but this dictionary requires a model item of type 'MenuRoles.Models.Menu'.
Answers (1)
2
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 8y
Try like below:
  1. DateTime dateTimeObj = DateTime.ParseExact(tbxFromDate.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture);  
  2.   
  3. if (tbxFromDate.Value != "" && dateTimeObj > DateTime.Today)         
  4. {        
  5.        Messagebox.Show("From Date should be earlier or equal To Today Date", MessageHelper.MessageType.Warning);  
  6. }  
 
Accepted
2
Amresh S

Amresh S

NA 528 4.1k 8y
Hi Raja,
 
You have to parse the date value before validation. Use the below code:
  1. DateTime date = DateTime.ParseExact(this.myTextBox.Text, "dd/MM/yyyy"null);  
Here you can send the  text value entered by the user or a value picked up from your datePicker control. The next key thing is to format your Date to be parsed and if time doesn't bother you, pass the "null" value.
 
Hope this helps.
 
Regards,
Amresh S. 
1
Manas Mohapatra

Manas Mohapatra

NA 29.3k 3.3m 8y
What is the value you are getting in tbxFromDate.Value?
0
Raja

Raja

NA 1.7k 45.3k 8y

Manas Mohapatra

Amresh S

Thank You so much!
0
Raja

Raja

NA 1.7k 45.3k 8y

Manas Mohapatra

22/11/2016