Format query to send to Viewbag

Asked

Viewed 114 times

0

I did a query in Latin, but the result was not as expected, it seems simple the problem but I can not solve.

Result on the label:

{ Year = 2016/2017 }

Expected result on label:

2016/2017

Controller:

//query
     var queryAnoPastoral = (from a in db.AnoPastoral
                                        orderby a.AnoPastoralID descending
                                        select new { a.Ano }).First();

       ViewBag.AnoPastoral = queryAnoPastoral.ToString();

View:

@Html.Label((string)@ViewBag.AnoPastoral, htmlAttributes: new { @class = "control-label col-md-2" })

1 answer

4


Do you want to return only the value of the right Year? The way you did you return an anonymous object so when you use the .toString() it comes with the keys, you can change your query to return the Direct Year that will work the way you expect.

var queryAnoPastoral = (from a in db.AnoPastoral
                        orderby a.AnoPastoralID descending
                        select a.Ano).First();

ViewBag.AnoPastoral = queryAnoPastoral.ToString();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.