0

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)

2 Answers2

1

You have not shown the complete code but it appears you are rendering this in a foreach loop. This will generate duplicate id attributes (invalid html) and name attributes 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 a for loop (the model needs to be IList<T> or alternatively you can use a custom EditorTemplate. Note I don't understand what you if statement is doing (it makes no sense) and if you want to change the value of Värde to an empty string if its value is 0 or -1, then you do that in the controller before you pass the model to the view.

for(int i = 0, i < Model.Count; i++)
{
  @Html.HiddenFor(m => m[0].AggKPIID)
  @Html.TextBoxFor(m => m[0].Värde, new { @class = "kpiTextbox kpiTextbox-edit" })
  @Html.ValidationMessageFor(m => m[0].Värde)
  @Html.HiddenFor(m => m[0].Tidsperiod)
  @Html.HiddenFor(m => m[0].Id)
}

and always used strongly types helpers and don't try to update the name attribute (not that new { Name = "Värde" } would do anything anyway)

  • Hey . You're right about me using a foreach. I get what you are saying. This is from a partialview, inside a table. And This part is just for the textbox. About the strongly typed helpers, I believe I chaged them to fit my update viewmodel more. Since it was sent item.Värde, and The VM could only pick up Värde. – Simon Ågren Dec 02 '14 at 05:53
  • Your going to have problems if your using partial views to render controls in a form for postback to model that is a collection or contains a property which is a collection. You haven't posted enough code to be sure, but you should always use an `EditorTemplate` or a `for` loop for collections to ensure the controls are correctly named with indexers (`name="[0].AggKPIID"`, `name="[1].AggKPIID"` etc.) –  Dec 02 '14 at 05:59
  • This is inside a Ajax beginform. And Im not updating any view. Just using onsuccess and onfailure. So my action returns Json. I use jquery to show an "ok" image if update goes well. But youre right :) – Simon Ågren Dec 02 '14 at 06:03
0

You should be able to define it only once

@{var attrs = null;}
@if (item.Värde.ToString() == "-1" || item.Värde.ToString() == "0")
{
    attrs = new { Value = "", @class = "kpiTextbox kpiTextbox-edit", @id = "tb" + Id };
}
else
{
    attrs = new { @class = "kpiTextbox kpiTextbox-edit", @id = "tb" + Id };
}
@Html.TextBoxFor(modelItem => item.Värde, attrs)
<br />
@Html.ValidationMessageFor(modelItem => item.Värde)

I also don't recommend setting Name.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29