[英]Pass list of objects from view to controller
我想将对象列表从视图传递到控制器的操作。
我的目标是,我将显示所有产品详细信息的列表。 用户可以编辑任何数据(甚至是所有数据)。
因此,在编辑之后,我想将 object 的整个列表发布到控制器的操作中,我将决定继续进行。
我的示例代码是:-
public class API_VM
{
public API_VM()
{
elemetnNames = new HashSet<string>();
}
public List<XElement> Stoklar { get; set; }
public HashSet<string> elemetnNames { get; set; }
public string PaketAgirligi { get; set; }
public string PaketGenisligi { get; set; }
public string PaketUzunlgu { get; set; }
public string PaketYuksekligi { get; set; }
public string StokAded { get; set; }
public string UrunAciklama { get; set; }
public string Kategori { get; set; }
public string UrunMarka { get; set; }
public string UrunAdi { get; set; }
public string UrunFiyat { get; set; }
public string UrunDil { get; set; }
public string SkuKodu { get; set; }
public string ServisPolitikaNo { get; set; }
public string KargoSablonID { get; set; }
public string StokAzaltmaStrateji { get; set; }
public string KargoyaVerilis { get; set; }
public string UrunFoto1 { get; set; }
public string UrunFoto2 { get; set; }
public string UrunFoto3 { get; set; }
public string UrunFoto4 { get; set; }
public string UrunFoto5 { get; set; }
public string UrunFoto6 { get; set; }
public string ParentElement { get; set; }
public string TedarikciLink { get; set; }
}
``
public class API_List:PageModel
{
[BindProperty]
public List<API_VM> ApiList { get; set; }
}
@model API_List @{ ViewData["Title"] = "UrunlerListe"; Layout = "~/Views/Shared/_Layout.cshtml"; int i = 0; } <<form class="form-horizontal" method="post" asp-action="UrunPost"> <table class="table table-striped"> <tr> <th>Product Name</th> </tr> @foreach (var item in Model.ApiList) { <tr> <td> <input type="text" asp-for="ApiList[i].UrunAdi" value="@item.UrunAdi" /> </td> </tr> } </table> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">Submit</button> </div> </div> </form>
[HttpPost]
public IActionResult UrunPost(API_List model) */ Model is always null.
{
}
Model 使用@model List<API_VM>
而不是@model API_List
。
@model List<API_VM>
@{
ViewData["Title"] = "UrunlerListe";
Layout = null;
}
<form class="form-horizontal" method="post" asp-action="UrunPost">
<table class="table table-striped">
<tr>
<th>Product Name</th>
</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
<input type="text" asp-for="@Model[i].UrunAdi" value="@Model[i].UrunAdi" />
</td>
</tr>
}
</table>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
在 controller 中,您可以使用List<API_VM>
。
[HttpPost]
public ActionResult UrunPost(List<API_VM> model)
{
API_List list = new API_List();
list.ApiList = model;
return View("~/Views/API_VMs/Index.cshtml", list);
}
测试截图:
更新
您可以使用 IFormCollection 检查FOREACH
和FOR
循环的返回类型的差异。
[HttpPost]
public string UrunPost(IFormCollection collection)
{
//
}
这是另一种解决方案,它也可以解析类型,但如果 class 有太多属性无法转换,则不推荐。
[HttpPost]
public ActionResult UrunPost(IFormCollection collection)
{
var apivms = FormCollectionToAPIVM(collection);
return View("~/Views/API_VMs/Index.cshtml", apivms);
}
private API_List FormCollectionToAPIVM(IFormCollection formCollection)
{
API_List apis = new API_List();
for (var i = 0; i < formCollection.Count; i++)
{
string key = $"ApiList[{i}].UrunAdi";
if (formCollection.ContainsKey(key)) {
var item = new API_VM();
item.UrunAdi = formCollection[key];
apis.ApiList.Add(item);
}
}
return apis;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.