For some reason, whenever I try to remotely validate a primitive property inside an array within my model, the value of the property is not passed as a parameter.
For example, when my remote validation method (UniqueItemNo) is called, the string parameter "id" is always null. If I were to perform validation on CartNumber instead of ItemNumber, the parameter is passed correctly.
Controller
public class HomeController : Controller
{
public ActionResult Index()
{
CartModel cart = new CartModel();
cart.Items.Add(new ItemModel() { ItemNumber = "12345" });
return View(cart);
}
[HttpPost]
public JsonResult UniqueItemNo(string id)
{
/** Do Work **/
return null;
}
}
Models
public class ItemModel
{
[Remote("UniqueItemNo", "Home", HttpMethod="POST")]
public string ItemNumber { get; set; }
}
public class CartModel
{
public CartModel()
{
Items = new List<ItemModel>();
}
public List<ItemModel> Items { get; set; }
public string CartNumber { get; set; }
}
View
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
@using(Html.BeginForm()) {
<p>
@for(int i = 0; i < Model.Items.Count; i++)
{
@Html.TextBoxFor(m => m.Items[i].ItemNumber);
@Html.ValidationMessageFor(m => m.Items[i].ItemNumber);
}
</p>
}
Try changing the parameter to match the model. Like this:
You can still start the name of the
ActionMethod
parameter with lowercase (itemNumber
instead ofItemNumber
) as per the normal coding conventions.EDIT: Have you tried coding the
View
like this?