0

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>
}
keshav
  • 866
  • 11
  • 19
Sparkle
  • 2,459
  • 3
  • 18
  • 20
  • The number of textbox is a number dynamical given by Controller or depends of how many want add the user? – Zach dev Jun 07 '12 at 19:42
  • depends on how many the user wants to add! For now I just would like a working simple example – Sparkle Jun 07 '12 at 20:54
  • Can you inspect the ShopName and Product textbox and see what the name is set to? also i think in your controller action it should be `string Product` not `List Product` – keshav Jun 08 '12 at 00:21
  • Product_0__Product and Product_1__Product – Sparkle Jun 08 '12 at 09:59

1 Answers1

0

look at the following answer. I would make product a class on its own like you tried. Loot at the rendered html code for the page and check out the field name for the ShopName textbox. I think it should be ShopName, if so you dont need to change the AdditionalFields attribute if not change it to the name rendered.

So something like this:

public class Shop
{            
        public string ShopName { get; set; }

        public List<Products> Product { get; set; }
}


public class Products
{

        [Remote("ProductExists", "Account", AdditionalFields = "ShopName", ErrorMessage = "Product is already taken.")]
        public String Product { get; set; }
}

in your view do something like this:

foreach(var item in Model.products)
{
   @Html.TextBoxFor(item => item.product) // not sure if the syntax is right 
}
Community
  • 1
  • 1
keshav
  • 866
  • 11
  • 19
  • I tried this but I'm not sure the ShopName value is visible to the Products class as it just passes a empty value all the time. – Sparkle Jun 07 '12 at 22:54
  • Nearly works! I'm pretty sure [Remote] has a bug in it as I have tired a million things and it always returns blank after the first item. – Sparkle Jun 08 '12 at 13:18