I have a container and nested viewmodels and use EditorFor to render nested viewmodels, i want to add remoteAttribute for validation to one of the properties in one viewmodel ( ConcreteViewModelA :: prop3). However, on the validation Controller action method, all i get is null.
I’ve tried using Validate([Bind(Prefix=”item”)]string prop3), but still comes back as NULL. any ideas?
public class SomeContainer
{
public List<ISomethingViewModel> SomeViewModels { get; set; }
}
public class ConcreteViewmodelA : ISomethingViewModel
{
public int prop1 { get; set; }
public int prop2 { get; set; }
[Remote("Validate", "RemoteValidation")]
public string prop3 { get; set; }
}
public class ConcreteViewModelB : ISomethingViewModel
{
public int prop1 { get; set; }
public int prop2 { get; set; }
}
public interface ISomethingViewModel
{
int prop1 { get; set; }
int prop2 { get; set; }
}
View:
@model test.Models.SomeContainer
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<p>Begin here</p>
foreach (var item in Model.SomeViewModels)
{
@Html.EditorFor(x => item)
}
}
Try defining a view model:
and then: