I’m using asp.net mvc4, simplemembership and EF.
actually i have this code to search a user in my db.
HomeController :
private UsersContext db = new UsersContext();
public ActionResult Index(string searchUser)
{
var user = from m in db.UserProfiles
select m;
if (!String.IsNullOrEmpty(searchUser))
{
user = user.Where(s => s.UserName.Contains(searchUser));
}
return View(user);
}
Index view :
<div id="searchUser">
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
@Html.TextBox("searchUser")
<input type="submit" value="Chercher" />
</p>
}
</div>
/********** PART 1 ***********/
@{
var db = new UsersContext();
var list = db.UserProfiles.ToList();
foreach (var item in list)
{
<table>
<tr>
<td>
@Html.DisplayFor(m => item.UserName)
</td>
</tr>
</table>
}
}
The part 1 here is obsolete. Instead of it, I would like to return the name of the user that I search for if he exist, otherwise I want to show a message to say he does not exist.
Actually, my request seem to work, but it returns nothing in the view, Is someone has an idea how can I solve this?
Thank you.
Try something like this,
on your controller, you always should return the same type for your view.
And type your view with the same type on the Action and read it to display a table…