[英]Passing a List<Model> as a RouteValue in RedirectToAction() in Asp.Net Core
我需要將一個基於 Model 的列表從一個 Controller 方法傳遞給另一個方法。 MailList
用於從一個Model(Family)中提取和過濾數據,另一個CreateMailingList
將結果保存到另一個Model(Contributie)中。
Model FilterListVM
是一個包含List<Familie>
的 ViewModel。 當重定向列表為空時,它應該包含三個記錄。
作為替代方案,我嘗試使用TempData
發送列表,但我收到了錯誤消息: InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer' cannot serialize an object of type 'System.Collections.Generic.List
1[DrieHamersV4.Models.Familie]'。
-simplified- controllerMethod 如下所示:
public async Task<IActionResult> MailingList(FilterListVM contributieLijst)
{
(...) // several variables that filter the data
(...)
string flexDefault = contributieLijst.FlexDefault;
int? contributieJaar = contributieLijst.ContributieJaar;
bool save = contributieLijst.Save;
string message = contributieLijst.Message;
var fList = contributieLijst.Ffamilie;
//************** Getting the Data and Filters for Standard Lists
switch (flexDefault)
{
case "contributie":
fList = await _context.Familie
.OrderBy(f => f.Roepnaam).ToListAsync();
break;
case (...)
break;
default:
fList = contributieLijst.Ffamilie; //previous selection
break;
}
}
contributieLijst.Ffamilie = fList; //fList contains 3 records
//******* Redirects to the next Method
if (save == true)
{
TempData["mailList"] = fList.ToList(); //gives an ErrorMEssage
return RedirectToAction("CreateMailingList", contributieLijst);
}
return View(contributieLijst);
}
public async Task<IActionResult> CreateMailingList(FilterListVM contributielijst)
{
var contributieJaar = contributielijst.ContributieJaar; // This works fine
//var mailingList = (List<Familie>)TempData["mailList"];
var mailingList = contributielijst.Ffamilie; //This is now empty
(...)
ViewModel FilterListVM
public class FilterListVM
{
public bool? Fovl { get; set; }
public string Fstaak { get; set; }
public string Fgeslacht { get; set; }
public bool? Fbloedverwant { get; set; }
public string Fland { get; set; }
public bool? Femail { get; set; }
public bool? Ftelefoon { get; set; }
public int? FleeftijdVan { get; set; }
public int? FleeftijdTot { get; set; }
public bool? Fcorrespondentie { get; set; }
public string SortColumn { get; set; }
public string SortOrder { get; set; }
public string FlexDefault { get; set; }
public bool ExportToggle { get; set; }
public string SearchString { get; set; }
public string CurrentFilter { get; set; }
public bool Reset { get; set; } = false;
public int? ContributieJaar { get; set; }
public bool Save { get; set; } = false;
public string Message { get; set; }
public List<Familie> Ffamilie { get; set; }
}
}
在添加到 TempData 之前,您可以將列表序列化為 json 字符串:
using System.Text.Json;
...
TempData["mailList"] = JsonSerializer.Serialize(fList);
然后在您的其他操作中:
var mailList = JsonSerializer.Deserialize<List<Familie>>(TempData["mailList"].ToString());
我按照你提到的那樣嘗試過,並在我的案例中重復了你的錯誤,
楷模:
public class FilterListVM
{
public FilterListVM()
{
Ffamilie = new List<Familie>();
}
public string Message { get; set; }
public List<Familie> Ffamilie { get; set; }
}
public class Familie
{
public int Id { get; set; }
public string Description { get; set; }
}
在 Controller 中:
public IActionResult SomeAction()
{
var familie1 = new Familie() { Id = 1, Description = "D1" };
var familie2 = new Familie() { Id = 2, Description = "D2" };
var familielist = new List<Familie>() { familie1, familie2, familie1 };
var vm = new FilterListVM() { Message = "hi" };
vm.Ffamilie = familielist;
return RedirectToAction("AnotherAction", routedic1);
}
public IActionResult AnotherAction(FilterListVM vm)
{
return Ok();
}
結果:
然后我嘗試創建一個新的 RouteValueDictionary 並添加代碼:
var routedic = new RouteValueDictionary();
Type vmtype = typeof(FilterListVM);
foreach (var property in vmtype.GetProperties())
{
routedic.Add(property.Name, vmtype.GetProperty(property.Name).GetValue(vm));
}
return RedirectToAction("AnotherAction", vm)
它的表現和以前一樣,所以在我看來,設計者可能不會考慮通過復雜的 model 包含一個列表來路由值,因為 url 的長度是有限的;
然后我嘗試如下硬編碼:
var routedic1 = new RouteValueDictionary(vm);
routedic1.Remove("Ffamilie");
for (int i = 0; i < vm.Ffamilie.Count; i++)
{
var key1 = string.Format("Ffamilie[{0}].Id", i.ToString());
routedic1.Add(key1, vm.Ffamilie[i].Id);
var key2 = string.Format("Ffamilie[{0}].Description", i.ToString());
routedic1.Add(key2, vm.Ffamilie[i].Description);
}
return RedirectToAction("AnotherAction", routedic1);
這次成功綁定了model,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.