0

我有一个页面添加了多次相同的输入框。

@Html.TextBoxFor(m => m.Product)
@Html.TextBoxFor(m => m.Product)
@Html.TextBoxFor(m => m.Product)

如何将它绑定到模型。

我试过了:

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

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

但我只能看到第一个字段中的数据。我也试过:

@Html.TextBoxFor(m => m.Product[0])
@Html.TextBoxFor(m => m.Product[1])
@Html.TextBoxFor(m => m.Product[2]) 

但是远程验证不起作用,所以我在这里有点难过。我想要实现的基本目标是将产品列表与商店一起发送,以便可以通过远程调用函数对其进行验证。我尝试将产品放在自己的公共类中,但后来我无法从该类中访问商店名称。

这是我正在尝试使用的控制器操作:

public JsonResult ProductExists(List<String> Product, string ShopName)

任何我如何解决这个问题的想法都会非常感激?

编辑

此 Semi 有效,但远程验证仍未通过 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; }
}

控制器动作:

public JsonResult ProductExists(List<String> Product, string ShopName)
    {

        return Json(true, JsonRequestBehavior.AllowGet);


    }

看法:

@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>
}
4

1 回答 1

0

看看下面的答案。我会像你尝试的那样让产品本身成为一个类。搜索页面呈现的 html 代码并检查ShopName文本框的字段名称。我认为应该是ShopName,如果是这样,如果不将AdditionalFields属性更改为呈现的名称,则不需要更改属性。

所以是这样的:

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; }
}

在您看来,请执行以下操作:

foreach(var item in Model.products)
{
   @Html.TextBoxFor(item => item.product) // not sure if the syntax is right 
}
于 2012-06-07T21:24:09.167 回答