0
votes

i have this code in my controller

[HttpGet]
public ActionResult Nabipour()
{
    string name = "Nabipour";
    var username = (from T in _db.tbl_teacher
                    where T.Page==name
                    select T.Username);
    ViewBag.Nabipour = from N in _db.tbl_Tpage
                        where N.Username.Equals(username)
                        select N;
    ViewBag.papers = from P in _db.tbl_Tpaper
                     where P.Username.Equals(username)
                     select P;

    return View();
}

and this is my view for this action:

@{
    ViewBag.Title = "Nabipour";
    Layout = "~/Views/Shared/_otherpage.cshtml";
}
....
<ul>
    @foreach (var paper in ViewBag.papers)
    {
        <li><a href="~/Content/Paper/@paper.PaperName"></a></li>
    }                 
</ul>
....

so as you see i not checking null in my select code and i tried this code with .FirstOrDefault() in the select. the error

Cannot compare elements of type 'System.Linq.IQueryable 1 . Only primitive types, enumeration types and entity types are supported

is on @foreach (var paper in ViewBag.papers) please help me what should i do?

1
Why are you using ViewBag instead of a strongly typed view with a proper model?DavidG
@DavidG because i need to use 2 model for this view .i not write the code yet. if it was just 1 model you right i was use a strongly typed view. but now i need ViewBag to send this information from both of modelsmina a.beigi
Use a view model with all the properties your need, not ViewBaguser3559349
Your error occurs because username is IQuerable, not a string. You need var username = (from T in _db.tbl_teacher where T.Page==name select T.Username).FirstOrDefault();user3559349
@StephenMuecke thanks i will check it now!mina a.beigi

1 Answers

1
votes
[HttpGet]
public ActionResult Nabipour()
{
    string name = "Nabipour";
    var username = (from T in _db.tbl_teacher
                    where T.Page==name
                    select T.Username).FirstOrDefault();
    ViewBag.Nabipour = from N in _db.tbl_Tpage
                        where N.Username.Equals(username)
                        select N;
    ViewBag.papers = (from P in _db.tbl_Tpaper
                     where P.Username.Equals(username)
                     select P).ToList();

    return View();
}