Reputation: 83
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)
}
}
Upvotes: 0
Views: 605
Reputation: 4361
check with firebug. Your url request looks like this Validate?item.prop3=
So you can do something like this to read the value
public ActionResult Validate(string prop3)
{
string prop3Val = Request.QueryString["item.prop3"].ToString();
//your operations with prop3Val
return Json(prop3Val, JsonRequestBehavior.AllowGet);
}
Upvotes: 0
Reputation: 1039140
Try defining a view model:
public class MyViewModel
{
public string Prop3 { get; set; }
}
and then:
public ActionResult Validate([Bind(Prefix = "item")] MyViewModel model)
{
return Json(
!string.IsNullOrEmpty(model.Prop3),
JsonRequestBehavior.AllowGet
);
}
Upvotes: 2