2

I am attempting to write a simple loop in a Razor view but the correct layout seems to have alluded me thus far.

My code is as follows:

    @using RelensterV3.Helpers

@Html.BeginForm("SaveStockAnswers", "Call", FormMethod.Post, new { onsubmit = "return false;" }) {
<div class="ui-grid-c">
    @{
        int Count = 1;
        string ClassToApply = "";
    }
    @foreach (var product in Model.StockAnswers)
    {

        if (Count == 1)
        {
            ClassToApply = "ui-block-a";
        }

        if (Count == 2)
        {
            ClassToApply = "ui-block-b";
        }

        if (Count == 3)
        {
            ClassToApply = "ui-block-c";
            Count = 0;
        }
        Count++;

        <div class="stock-product @ClassToApply">
            @*<h2>@product.Product.Name</h2>*@
            @Html.Partial("_StockAnswerForm", product)
        </div>   
    }
</div>
} 

However, the output of that is as follows:

<form action="/Call/SaveStockAnswers" method="post" onsubmit="return false;">System.Web.Mvc.Html.MvcForm {
<div class="ui-grid-c">
        <div class="stock-product ui-block-a">

            <input type="hidden" name="stock.index" autocomplete="off" value="f8befb95-b30d-45bd-bc0e-6214d2001769" />
    <div>.....etc etc

Which ends with a trailing curly brace. I'm not sure why these snippets are being output.

Thanks

1
  • thanks I was working with asp.net mvc and jquery mobile today and repurposed the code above. Commented Aug 25, 2011 at 20:10

2 Answers 2

5

I would use:

@using (Html.BeginForm(etc..))
{
    // Your loop code
}
Sign up to request clarification or add additional context in comments.

Comments

4

You are forgeting the using statement at the beginning of your html form declaration. Rewrite it as

@using(Html.BeginForm ... etc, etc

Comments

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.