1

I have a Model with a list element. I'm creating my page at runtime, with a 'foreach' loop that inserts all list elements in the page. I have also a string value that I want validate (model.trayCapacity) checking if it is NOT null:

[Required(ErrorMessage = "*")]
public string trayCapacity{ get; set; }
public IList<Separatore> SeparatoriLongitudinali{ get; set; }

This is .cshtml code:

@using (Html.BeginForm("Optional", "Config", FormMethod.Post, new { id = "storeDataForm" }))
{          
    @Html.ValidationSummary(true)

    foreach (Separatore sepLong in Model.SeparatoriLongitudinali)
    {
         @Html.TextBoxFor(x => sepLong.Quantity) 
    }

    <table> 
        <tr class="webgrid-row-style">                                
            <td>Tray Capacity:</td>
            <td>@Html.TextBoxFor(model => model.trayCapacity)</td>  
            <td>@Html.ValidationMessageFor(model => model.trayCapacity)</td>
        </tr>
    </table>
    <div align="right">
        <input type="submit" value="buttonNext" />
    </div>  
}

There is a SyntaxError: JSON.parse: unexpected character, in jquery.js (version 1.9.1), and the page goes on without checking the value.

ValidationSummary works only if I cut off the "foreach" code. Why is that?

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
gorgonzola
  • 115
  • 2
  • 12
  • This is html code for my **@Html.TextBoxFor(x => sepLong.Quantity)**: but i don't want to verify this value with ValidationSummary! Why this textbox has been created with these tags (data-val data-val-number data-val-required)? – gorgonzola Apr 15 '13 at 08:13

0 Answers0