ASP.NET Core中如何通过ActionResult向视图传字符串列表并用于表单
解决方法:在视图中获取导入数据并用于表单提交
你的当前代码中已经通过_importManager.ImportPhonenumbersFromXlsxAsync获取了导入结果result,但没有将其传递到视图。以下是几种实用的实现方式,根据你的场景选择:
方案1:通过TempData传递数据(适配重定向场景)
因为你当前使用RedirectToAction跳转到Edit页面,这是跨请求操作,ViewData/ViewBag会失效,所以用TempData存储跨请求数据最合适。
步骤1:在ImportAction中存储导入结果
修改你的ImportPhonenumbersFromExcel方法,将导入结果序列化后存入TempData:
[HttpPost] public virtual async Task<IActionResult> ImportPhonenumbersFromExcel(IFormFile importexcelfile, int currentFestivalId) { // 权限校验部分保持不变 if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageFestivals)) return AccessDeniedView(); try { if (importexcelfile != null && importexcelfile.Length > 0) { var result = await _importManager.ImportPhonenumbersFromXlsxAsync(importexcelfile.OpenReadStream()); // 将结果序列化存入TempData(需引入System.Text.Json) TempData["ImportResult"] = JsonSerializer.Serialize(result); } else { _notificationService.ErrorNotification(await _localizationService.GetResourceAsync("Admin.Common.UploadFile")); return RedirectToAction("Edit", new { id = currentFestivalId }); } _notificationService.SuccessNotification(await _localizationService.GetResourceAsync("Admin.Festival.Phonenumbers.Imported")); return RedirectToAction("Edit", new { id = currentFestivalId }); } catch (Exception em) { await _notificationService.ErrorNotificationAsync(em); return RedirectToAction("Edit", new { id = currentFestivalId }); } }
步骤2:在EditAction中读取TempData并传递到视图
在Edit页面的Get请求方法中,取出TempData中的数据并绑定到视图模型或ViewData:
public async Task<IActionResult> Edit(int id) { // 原有逻辑:获取Festival编辑数据 var festival = await _festivalService.GetFestivalByIdAsync(id); var viewModel = new FestivalEditViewModel { FestivalId = festival.Id, // 其他原有属性赋值 }; // 读取导入结果 if (TempData.ContainsKey("ImportResult")) { var importResultJson = TempData["ImportResult"] as string; viewModel.ImportResult = JsonSerializer.Deserialize<ImportResultDto>(importResultJson); // 如果没有ViewModel,也可以存入ViewData:ViewData["ImportResult"] = importResult; } return View(viewModel); }
步骤3:在视图中生成表单提交控件
在Edit视图中,读取导入结果并生成隐藏域(或其他控件),以便后续表单提交时携带数据:
@model FestivalEditViewModel <!-- 原有表单内容 --> <form asp-action="SubmitImportedData" method="post"> <input type="hidden" name="FestivalId" value="@Model.FestivalId" /> @if (Model.ImportResult != null) { <!-- 示例:将成功导入的号码ID列表转为逗号分隔的字符串存入隐藏域 --> <input type="hidden" name="ImportedPhoneNumberIds" value="@string.Join(",", Model.ImportResult.SuccessIds)" /> <!-- 或者循环生成单个隐藏域,适合批量数据提交 --> @foreach (var phoneNumber in Model.ImportResult.SuccessNumbers) { <input type="hidden" name="PhoneNumbers" value="@phoneNumber" /> } <button type="submit" class="btn btn-primary">确认保存导入的号码</button> } </form>
方案2:直接返回结果视图(无需重定向场景)
如果你不需要跳回原Edit页面,而是想直接展示导入结果并提供提交按钮,可以修改Action返回指定视图:
修改ImportAction:
// 替换原有的RedirectToAction代码 // return RedirectToAction("Edit", new { id = currentFestivalId }); ViewBag.CurrentFestivalId = currentFestivalId; return View("ImportResult", result);
创建ImportResult视图:
@model ImportResultDto <h3>导入完成</h3> <p>成功导入@Model.SuccessCount条号码,失败@Model.FailedCount条</p> <form asp-action="SubmitImportedData" method="post"> <input type="hidden" name="CurrentFestivalId" value="@ViewBag.CurrentFestivalId" /> @foreach (var number in Model.SuccessNumbers) { <input type="hidden" name="PhoneNumbers" value="@number" /> } <button type="submit">确认提交</button> </form>
关键注意事项
- 确保
ImportResultDto是可序列化的类(无循环引用、属性为公共可访问)。 - 如果导入结果数据量较大,建议只传递关键信息(如ID列表)而非完整实体,避免性能问题。
- 提交表单时,对应的Action需要接收这些参数(如
List<string> PhoneNumbers或string ImportedPhoneNumberIds)。
内容的提问来源于stack exchange,提问作者Aref_Za
相关产品推荐
相关产品推荐

