Answer the question
In order to leave comments, you need to log in
How to display a List in a View if the list is still null?
Good afternoon!
I'm mastering asp.net mvc, a completely newbie developer.
My task is this: From View I get some kind of string, I look for a match in the database and display the result below.
Here is the controller code:
[HttpPost]
public ActionResult Index(string surname)
{
var allDriver = db.Table.Where(a => a.surname.Contains(surname)).ToList();
if (allDriver.Count != 0)
{
ViewBag.Drivers = allDriver;
return View();
}
return View();
}
<div>
@using (Html.BeginForm())
{
@Html.TextBoxFor(x=>x.surname);
<input type="submit" value="Найти"/>
}
@foreach (var x in @ViewBag.Drivers)
{
<p>@x</p>
}
</div>
Answer the question
In order to leave comments, you need to log in
1) Master the concept of Model. Create a class one of whose fields will be your Drivers. Create a strongly typed View for this class. (At the same time, learn how to load this list asynchronously)
2) An easier way "on the forehead"
@if(ViewBag.Drivers!=null)
{foreach (var x in ViewBag.Drivers)
{
<p>@x</p>
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question