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?

scott-pascoe
  • 1,463
  • 1
  • 13
  • 31
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56

2 Answers2

4

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>
ssilas777
  • 9,672
  • 4
  • 45
  • 68
  • 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 – ɐsɹǝʌ ǝɔıʌ Mar 07 '13 at 13:44
  • But both the way should work I tested this in my MVC3 as well as MVC4 apps. – ssilas777 Mar 07 '13 at 13:52
  • What's the result you getting when trying this? and are you trying this in same view? – ssilas777 Mar 07 '13 at 13:54
  • 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 – ɐsɹǝʌ ǝɔıʌ Mar 07 '13 at 14:11
  • 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!! :) – ɐsɹǝʌ ǝɔıʌ Mar 07 '13 at 14:51
0

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

msarchet
  • 15,104
  • 2
  • 43
  • 66