我有一个页面,其中添加了相同的输入框。
@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)
任何想法如何解决这个问题都会非常感激?
修改
这半可以使用,但远程验证仍未通过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>
}
答案 0 :(得分:0)
查看以下answer。我会像你试过的那样让产品成为一个类。点击页面的呈现的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
}