I have a page with the same input box added a number of times.
@Html.TextBoxFor(m => m.Product)
@Html.TextBoxFor(m => m.Product)
@Html.TextBoxFor(m => m.Product)
How to I bind this to the Model.
I've tried:
public class Shop
{
public string ShopName { get; set; }
[Remote("ProductExists", "Account", AdditionalFields = "ShopName", ErrorMessage = "Product is already taken.")]
public List<String> Product { get; set; }
}
But I can only ever see the data in the first field. Also I tried:
@Html.TextBoxFor(m => m.Product[0])
@Html.TextBoxFor(m => m.Product[1])
@Html.TextBoxFor(m => m.Product[2])
But remote validation doesn't work so I'm a little stumped here. Essential what I would like to achieve is to send the list of products with the shop so that it can be validated via a remote call to a function. I tried putting the products within there own public class but then I wasn't able to access the shop name from within that class.
This is the Controller Action I'm trying to use:
public JsonResult ProductExists(List<String> Product, string ShopName)
Any Ideas how I could solve this would be so much appreciated?
EDIT
This Semi works but remote validation still isn't passing ShopName:
public class Shops
{
[Required]
public string ShopName { get; set; }
public List<Products> Product { get; set; }
}
public class Products
{
[Required]
[Remote("ProductExists", "Home", AdditionalFields = "ShopName", ErrorMessage = "Product is already taken.")]
public String Product { get; set; }
}
Controller Action:
public JsonResult ProductExists(List<String> Product, string ShopName)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
View:
@model Shop.Models.Shops
@{
ViewBag.Title = "Shop";
}
<h2>Shop</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Shop</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ShopName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ShopName)
@Html.ValidationMessageFor(model => model.ShopName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Product[0])
@Html.ValidationMessageFor(model => model.Product[0])
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Product[1])
@Html.ValidationMessageFor(model => model.Product[1])
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Product[2])
@Html.ValidationMessageFor(model => model.Product[2])
</div>
<input type="submit" value="Create" />
</fieldset>
}