RedirectToAction in Loop process [.Net Core 3.1]



我一直在努力完成我正在开发的应用程序

这是一个场景:

  1. 我有一个Razor页面,用户将在其中输入客户编号、公司代码和日期。用户可以输入多个客户号码,应用程序会用逗号分隔
  2. 一旦用户输入了详细信息,一个带有asp操作的按钮指向名为GenerateSoa的操作,它将运行一个foreach语句,该语句将为GUI上输入的每个客户RedirectToAction

问题从这里开始,当循环运行时,即使有3个客户输入,它也只打开一个选项卡。它应该打开3个选项卡,其中包含3个不同客户的详细信息。下面是我的代码

但是,我没有包含SoaLoopercshtml文件。

SoaController.cs

public IActionResult GenerateSoa()
{
ClearAmounts();
#region Date management for SOA
// First day of Current Month
var FirstDateOfCurrentMonth = new DateTime(SD.DateToday.Year, SD.DateToday.Month, 1);
var PreviousMonthFirstDay = FirstDateOfCurrentMonth.AddMonths(-1);
var PreviousMonthLastDay = DateTime.DaysInMonth(SD.DateToday.Year, PreviousMonthFirstDay.Month);
****** Ommitted some code *****
// Get last day of Previews month
var PreviewsBalanceDate = PreviousMonthFirstDay.Month.ToString() + "/" + 
PreviousMonthLastDay.ToString() + "/" + PreviousMonthFirstDay.Year.ToString();
#endregion Date management for SOA
//SD.GuiCustomerNum = customer.ToString();
var bsid_unpaid_payments = _context.BSIDs.Where(l => 
(l.UMSKZ == "" || l.UMSKZ != "C") && l.BLART == "DJ");
foreach (var payments in bsid_unpaid_payments)
{
SD.PAmount += Convert.ToDouble(payments.DMBTR);
}
SD.UPTotalAmount = SD.UPAmount - SD.PAmount;
return View();
}
public IActionResult SoaLooper(string customer, int company, DateTime asof)
{
string[] customerNum = customer.Split(',');
SD.GuiCompany = company.ToString();
SD.DateToday = asof;
foreach (var item in customerNum)
{
SD.GuiCustomerNumSelected = item.ToString();
RedirectToAction(nameof(GenerateSoa));
}
return View();
}

索引.cshtml

<div class="container h-100">
<div class="row h-100 justify-content-center align-items-center border">
<form method="post" class="col-12 text-center">
<div class="col-12 border-bottom">
<h2 class="text-primary">Statement of Account</h2>
</div>
<div class="col-8 pt-4">
<div class="form-group row">
<div class="col-4">
<label class="float-right">Customer</label>
</div>
<div class="col-8">
<input id="customer" name="customer" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label class="float-right">Company Code</label>
</div>
<div class="col-8">
<select id="company" name="company" class="form-control">
<option value="">Select a number</option>
<option value="2000">2000</option>
<option value="3000">3000</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label class="float-right">Statement as of</label>
</div>
<div class="col-8">
<input id="asof" name="asof" type="date" class="form-control" />
</div>
</div>
<div class="form-group row">
<div class="col-8 offset-4">
<div class="row">
<div class="col">
<button type="submit" formtarget="_blank" id="btnCheck" 
class="btn btn-primary form-control" asp-action="SoaLooper">Generate</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>

RedirectToAction没有打开新的选项卡,它只是返回一个302的状态代码,告诉客户端重定向。

如果你真的想打开多个新的选项卡,你应该做一些事情,比如返回一个url列表,然后在页面加载时使用window.open。

我还没有测试过,但你可以这样做:

将您的url添加到列表中:

SoaController.cs

var newTabUrls = new List<string>();
foreach (var item in customerNum)
{
SD.GuiCustomerNumSelected = item.ToString();

newTabUrls.Add(nameof(GenerateSoa));
}
return View(newTabUrls); 

Index.cshtml

<script type="text/javascript">
@if(Model?.Any() ?? false)
{
@foreach(var url in Model)
{
@:window.open(url, "_blank");
}
}
</script>

在新选项卡(而不是新窗口(中打开URL

如何在asp.net中点击按钮在新选项卡中打开页面?

最新更新