Error while fetching data from database laravel 5.1 - laravel-5.1

I am new to laravel. I am facing error while i fetch the data from database. But other pages are working (fetching also).
Here is My code for your review.
my contorller :
public function info()
{
// Show the page
$data = Customer::all();
return view('eventlist')->with('data',$data);
}
My view :eventlist.blade.php
<tbody>
#foreach ($events as $user)
<tr>
<td>{!! $user->name !!}</td>
<td>{!! $user->cname !!}</td>
<td>{!! $user->sdate !!}</td>
<td>{!! $user->edate !!}</td>
<td>{!! $user->room !!}</td> </tr>#endforeach
</tbody>
My route :
Route::get('eventlist', 'MyController#info');`
While i try to fetch the data : It shows the error undefined variable :events
Help me to solve this issue. Thanks in advance

You are looping for wrong variable, look at your this line
#foreach ($events as $user)
you are looping $events and from controller you are passing data variable. see your this line
return view('eventlist')->with('data',$data);
so change your this line
#foreach ($events as $user)
to this
#foreach ($data as $user)
everything will be fine

Related

How to retrive data from ViewData in foreach loop

Hi I have get records in ViewData["TutorList"] = GetData.Tables[0];
Here GetData DataSet and in that table[0] is bind with ViewData["TutorList"]
#foreach (var item in (DataTable)ViewData["TutorList"])
{
<tr>
<td>#item["Name"].ToString()</td>
</tr>
}
How Can I get all the records in foreach
The following code should give you want you want
#foreach (DataRow item in ((DataTable)ViewData["TutorList"]).Rows)
{
<tr>
<td>#item["Name"].ToString()</td>
</tr>
}
But I would suggest that you use a view model specific to the view and use the associated Html helpers in your view.

Getting null or empty when i want to show image from data in MVC

I want to dispaly image from database in Mvc but it show me null or empty error. Value cannot be null or empty. Parameter name: contentPath.
Please tell me any one what are my mistake in this code:
#foreach (tbl_AdminManageProducts item in #Model)
{
<tr>
<td>#item.CategoryCode</td>
<td>#item.ProductName</td>
<td>#item.ProductPrice</td>
<td>#item.ProductDetail</td>
<td><img src="#Url.Content(item.ProductImage)" /></td>
<td>#item.CompanyName</td>
</tr>
}
This is controller:
public class AdminController : Controller {
// GET: /Admin/
public ActionResult Index() {
FYP_WISHEntities db = new FYP_WISHEntities();
return View(db.tbl_AdminManageProducts.ToList());
}
}
Looks like you are trying to call the Url.Content method with a NULL value. The possible reason for this could be a null value is returned from the expression item.ProductImage for one of the iteration of your loop. So adding a null check would fix your error.
<td>
#if(String.IsNullOrEmpty(item.ProductImage))
{
<img src="#Url.Content(item.ProductImage)" />
}
</td>
This code will try to render the image tag markup only if your item has a non null/non empty ProductImage property value.
Url.Content method takes a valid content path to the file. So make sure your item.ProductImage returns that when not null or empty.

Why is DisplayForModel not showing the expected output?

I have a simple razor page, where the first part correctly shows information from each element in my model in a table. After that I'm trying to use DisplayFor to show me every property in the model, but it generates 123 for each iteration. What am I doing wrong?
#model System.Data.Entity.IDbSet<DBFramework.MyPerson>
#{
ViewBag.Title = "Home Page";
}
<p></p>
<table border="1">
<thead>
<tr>
<th style="width:300px;">Name</th>
<th style="width:300px;">Age</th>
</tr>
</thead>
#foreach (var p in Model)
{
<tr>
<td>#Html.Label(p.Name)</td>
<td>#Html.Label(p.Age.ToString())</td>
</tr>
}
</table>
#foreach (DBFramework.MyPerson x in Model)
{
#Html.DisplayForModel(x)<br/>
}
Creates the following output:
Name Age
Mike 40
Penny 1
Kunal 30
123
123
123
Try this:
#foreach (var item in Model)
{
#Html.DisplayFor(model => item)
}
DisplayFor and DisplayForModel are used with templates in Views\Shared\DisplayTemplates.
When you call DisplayForModel, it will take the page model (Model) and if you specify a parameter, will try to load the template with the name provided (if string) or add the object as additional data in ViewData. When you called:
#foreach (DBFramework.MyPerson x in Model)
{
#Html.DisplayForModel(x)<br/>
}
You told it - Display Model which is type System.Data.Entity.IDbSet<DBFramework.MyPerson> and add MyPerson in the template ViewData. Do this 3 times. You also didn't give him a hint to select a DisplayTemplate and it will try to use convention to find it.
As Bappi Datta said, if you want to display a template for MyPerson, you need to call:
#foreach (var item in Model)
{
#Html.DisplayFor(model => item)
}
However, you need a MyPerson display template created which accepts #model DBFramework.MyPerson to render the item or it will simply do a .ToString() by default I believe.

pass model to view using viewbag

i want to send two model in my view
and in other page maybe and need more
controller
ViewBag.Users = db.Users.ToList();
and in View
#using Documentation.Models
#{
var Users = (Documentation.Models.User)ViewBag.Users;
}
<table>
#foreach (var item in Users) {
<tr>
<td>
#Html.DisplayFor(item.username)
</td>
</tr>
}
</table>
but i get no answer , i know my code is not right
i got this error
foreach statement cannot operate on variables of type 'Documentation.Models.User' because 'Documentation.Models.User' does not contain a public definition for 'GetEnumerator'
you should cast viewbag.model to List of User. Not to User.
#{
var Users = (List<User>)ViewBag.Users;
}

How does one avoid a NullReferenceException in a foreach loop within a View when my model is null?

I get a "NullReferenceException was unhandled by user code" error with the following code in my View when I pass in a null value via my controller. There are situations where I want to pass in a null value, but I do not want an error thrown when this happens. What should I change my code to?
Originally my code was:
#foreach (var item in Model.MyModelStuff)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Bla.Title)
</td>
<tr>
}
I have tried the following with no success:
#foreach (var item in Model.MyModelStuff.Where( item => item.MyModelStuff != null))
etc. . .
How do I change the code so that it will handle null without throwing an error? I've read I may need to be returning an empty collection of my model (?), how would I go about doing that - if it is indeed the necessary thing to do?
If my understanding is correct your collection is null.
A collection should never be null, like you said you should return an empty collection instead and prevent your collection to be corrupted not exposing the real collection:
public IList<Employee> Employees
{
get;
private set;
}
And initialize your collection inside the constructor
this.Employees = new List<Employee>();
Honestly, I think a null model is a poor choice. But if you insist, just add an if check:
#if (Model != null) {
foreach (var item in Model.MyModelStuff)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Bla.Title)
</td>
<tr>
}
}

Resources