0

I am trying to count the no of rows returned from linq to sql and passing its value from controlller to view through viewBag but its is giving an error Cannot convert type 'int' to 'string'

View

@Html.Label((string)ViewBag.totalSpaces)

Controller

List<RentOutSpace> parkingSpaces = searchModel.searchSpace(address);
int count = parkingSpaces.Count();
string countSpace = count.ToString();
ViewBag.totalSpaces = countSpace;
4
  • What is not clear? Cannot convert type 'int' to 'string Commented Apr 5, 2014 at 18:23
  • I want to assign value of viewBag to a label but how i can i achieve this cx cannot convert 'int' to string' Commented Apr 5, 2014 at 18:25
  • Everything here looks correct. Are you able to get a stack trace or use a debugger to determine the exact line the error is happening on? Commented Apr 5, 2014 at 18:26
  • You just missed the @ as the prefex of ViewBag Commented Apr 5, 2014 at 18:40

2 Answers 2

1

You don't need to use the @Html.Label

Just use

@ViewBag.totalSpaces

Or Html.Label((string)@ViewBag.totalSpaces)

This should work

Sign up to request clarification or add additional context in comments.

Comments

1

Try this one:

@Html.Label(ViewBag.totalSpaces)

5 Comments

From what we can see, what's being set into the ViewBag is a string anyway, and calling ToString() on a string doesn't do much. The question is currently unclear.
as i told that we cannot convert int to string in linq to sql.when i apply toString method it remains still as int when it is passed to viewBag.
@christos i trid u solution it is giving error Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
@Damien_The_Unbeliever you are right. Everything is set into the ViewBag is a string and for that reason the .ToString() is not needed.
@Wasfa what you are saying is not possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.