2

I´m working on a MVC3 project.

I already have a decimal value into a ViewData["nReceived"] calculated in the controller. After that the controller calls to Index view.

I'm able to display it on Index view outside of a element

<div class="bar bar-success" style="width: 42%;">Received  @ViewData["nReceived"]</div>

but I need to use this value as a property of element replacing the width percentage.

I tried this with no success:

<div class="bar bar-success" style="width: @ViewData["nReceived"]%;">Received</div>

and also this:

@ViewContext.Writer
    .Write("<div class=\"bar bar-success\" style=\"width: @ViewData["nReceived"]%;">")

Any ideas on how to get it working?

2 Answers 2

5

This should work use @()

<div class="bar bar-success" style="width:@(ViewData["nReceived"])%;">Received</div>

Also Since you are using MVC3, you can try ViewBag as well

<div class="bar bar-success" style="width:@(ViewBag.nReceived)%;">Received</div>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer. First option still not working. Using ViewBag i have the same problem that in origin. I can use it outside div element but not as a property
But both the way should work I tested this in my MVC3 as well as MVC4 apps.
What's the result you getting when trying this? and are you trying this in same view?
The result is that div element has no width, but if i use @(ViewBag.nReceived) or @(ViewData["nReceived"] outside the div element, the decimal value is displayed correctly, so i can be sure that it contains value
Sorry my mistake!!! I was trying to set a decimal value to the width property and this is not valid. Width property only allow entire values. Inserting a int32 value instead decimal into Viewbag or Viewdata works like a charm!! Thank you so much!! :)
0

style='width: @Html.Raw(ViewData["nReceived"])' you're breaking your quotes and you need this to render out a little differently here

4 Comments

Thanks for your answer. I have not .ToRaw() in the @Html helper :(
you are correct it should be Raw, which version of MVC are you using?
I´m using MVC3 on Visual Studio 2010
Finally i was able to find Raw an d tried this <div class="bar bar-danger"style="width: @Html.Raw(System.Convert.ToString(ViewData["nReceived"]))%;"></div> since Raw requires a string. No luck :(

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.