I'm not getting all my textboxes's validation to work. Only while using "one" at a time. I'm trying to use the same model property for of my variations. Does anyone know a solution to this, or a better way of doing this?
@Html.Hidden("AggKPIID", item.AggKPIID)
@if (item.Värde.ToString() == "-1" || item.Värde.ToString() == "0")
{
@Html.TextBoxFor(modelItem => item.Värde, new { Value = "", Name = "Värde", @class = "kpiTextbox kpiTextbox-edit", @id = "tb"+Id })
<br />
@Html.ValidationMessageFor(modelItem => item.Värde)
}
else
{
@Html.TextBoxFor(modelItem => item.Värde, new { @class = "kpiTextbox kpiTextbox-edit", @id = "tb" + Id })
<br />
@Html.ValidationMessageFor(modelItem => item.Värde)
}
@Html.Hidden("Tidsperiod", item.Tidsperiod)
@Html.Hidden("Id", Id)
You have not shown the complete code but it appears you are rendering this in a
foreachloop. This will generate duplicateidattributes (invalid html) andnameattributes so it wont post back to your collection anyway. Because the name attributes are all the same, the associated validation errors cannot be matched up. Change you view to use aforloop (the model needs to beIList<T>or alternatively you can use a customEditorTemplate. Note I don't understand what youifstatement is doing (it makes no sense) and if you want to change the value ofVärdeto an empty string if its value is0or-1, then you do that in the controller before you pass the model to the view.and always used strongly types helpers and don't try to update the
nameattribute (not thatnew { Name = "Värde" }would do anything anyway)