0

I have a model with an attribute int. This attribute is a value that I need to count a forloop.

//Markup
@{var value = Html.DisplayFor(m => m.periodCounter).ToString();}
@for (var i = 0; i < Convert.ToInt32(value); i++)
{
//code here
}

I get the errormessage "Input string was not in a correct format". Is this possible to solve? All I really need to do is declaring a variable with the value of a textbox (in the same view). Right now I have a TextBoxFor and as you can see I'm trying to get and convert it with DisplayFor

9
  • What are you getting for value? If there are extra spaces, you might need to do a .Trim() Commented Mar 14, 2014 at 14:47
  • I think it is MvcHtmlString Commented Mar 14, 2014 at 14:48
  • What are you trying to achieve? Why are you converting a variable to HTML and then to an int again? Commented Mar 14, 2014 at 14:50
  • Tried with trim as well. The application crashes as soon as the view is loaded so I don't have the time to fill the model. Is that possible the issue? Commented Mar 14, 2014 at 14:50
  • @OscarPaz I'm having a textbox where the users should put in a value (int) and that value do I need to count a forloop in the same view Commented Mar 14, 2014 at 14:51

1 Answer 1

2

Um...

for (var i = 0; i < Model.periodCounter; i++)

?

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

5 Comments

The object is not created when the page loads (it's all in the same view). So I get "Object reference not set to an instance of an object"
Then your Html.DisplayFor(...), method will fail, too.
@OscarPaz oh..of course. But is it suppose to be so hard to just give me a int value from textbox to a C# var in the same view?
@krillezzz depending on the timing you're thinking of, it could be completely impossible. Remember that the C# is server-side, and the only code you can run client-side is JS.
@TimS. I will rewrite everyting in jquery and try. Inside the loop I'm supposing to make a table and the code is (for me) much easier to write when mixing C# in markup instead of jquery

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.