Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Return file but clear Model State C#

Hello so i download a File but if it doesent find the right values there will be an error Message. Now after entering the correct Data the File is sucesfully downloaded, but the error Message is still there. I have a Button Generate Report that calls this function.

<div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <br /><br />
                <input type="submit" name="generateReport" value="Generate Report" class="button button--small button--primary" />
            </div>
        </div>
public ActionResult Index(ReportingModel reportingModel, string aaa)
        {
            if (reportingModel.FromDate > reportingModel.ToDate)
            {
                ModelState.AddModelError("CustomError", "Date range not correct");
                return RedirectToAction("Index", reportingModel);
            }
            var path = Server.MapPath(@"~/App_Data/Templates/File.xlsx");
            var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            var liste = reportingService.GetMarketRiskData(reportingModel.FromDate, reportingModel.ToDate, reportingModel.BenchmarkIds, reportingModel.TenorIds);
            if (liste.Count() > 0)
            {
                var reportDataStream = reportingService.GenerateReport(fileStream, liste);
                ModelState.Clear();
                ViewBag.JavaScriptFunction = "Hello";


                return File(reportDataStream.ToArray(), "application/xlsx", $"{DateTime.Today:yyyyMMdd}_RAD_MarketRisk.xlsx");
            }
            ModelState.AddModelError("CustomError", "NO rows found in given selection");
            FillViewBagData();
            return View(reportingModel);
        }

Ad you can see i try to Clear the Model State but because the Page doesent Refresh the model state isnt cleared. There would be some ideas to refresh the page later via JavaScript but that could be inconsistent.

Thanks for the Help:)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!