我有一个名为ProductSupplier的模型
我将@model IEnumerable传递给我的View
并从视图中显示 现在,当我提交表单时,我没有在我的http post方法中获得IEnumerable的列表。我想知道用户选择的供应商。
以下是我的模特
public sealed class ProductSupplier
{
public int CountryId { get; set; }
public int UserId { get; set; }
public bool IsProductSupplier { get; set; }
public string CountryName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
这是我的HttpGet方法
public ActionResult ManageSupplier(int id)
{
var supplier = App.UsersRepo.GetSupplierForProduct(id);
return View(supplier);
}
我通过以下方式绑定它(你可以建议我最好的方式,我是MVC的新蜜蜂)
@model IEnumerable<ProductSupplier>
@using (Html.BeginForm("ManageSupplier", "Products", FormMethod.Post, new { role = "form") })
{ @Html.AntiForgeryToken()
foreach (var item in Model)
{
<div class="checkbox">
<label>
@Html.CheckBoxFor(x => item.IsProductSupplier, new { id = item.Email }) @item.FirstName @item.LastName (@item.Email)
</label>
</div>
}
}
最后我的HttpPost方法
[HttpPost]
public ActionResult ManageSupplier(IEnumerable<ProductSupplier> obj)
{ // I m getting obj null in my argument
//I want to Get selected id from obj and want to pass in selectedSupplier
var returnVal = App.ProductRepo.AssigneSupplierForProduct(productId, selectedSupplier);
return Json(new { success = true }, JsonRequestBehavior.DenyGet);
}
任何人都可以建议我在哪里犯错误。 我是MVC的新手任何一种高度赞赏的建议 先感谢您。
答案 0 :(得分:-2)
Firstable你不能这样做。其中一种方法是这样的。这是你做这件事的基本步骤。
1-assign for all复选框,复选框更改事件具有唯一ID。 (看看here)
2-Cretae jquery对象并在单击复选框时存储数据,反之亦然
var ListProductSuppliers ={ {ProductSupplier_info_here },{ProductSupplier_info_here } };
3 - 稍后通过ajax请求,对此对象进行serilize(ListProductSuppliers
)并发送到您的方法
4-on服务器端将其设置为IEnumerable<ProductSupplier>
5以后随意使用那些选定的支持者