Reputation: 393
I have an application in ASP.NET MVC with a simple form which reads data from a file and shows the data in the form in browser.The issue is that every time I refresh the browser,the data from the file is added again in the front end.This is the controller where I have the issue:
public ActionResult Main(BankingModel _bm)
{
ViewBag.List = IO.Read();
return View(_bm);
}
IO.Read reads the data from the file,stores it in the Viewbag and send it to the view.But the controller is called every time I refresh the page and the data from the browser remains,resulting in duplicated values.Is there a way,when I refresh thee browser,to also refresh the front end view data from the form?
Upvotes: 0
Views: 504
Reputation: 8312
Regarding your particular case, you were not intiailzing a new instance of your List<BankingModel>
which caused the data to be appended to the list always. The solution to this would be to initialize a new instance of your list inside the Read
method:
public static List<BankingModel> lst = new List<BankingModel>()
And in order to clear your ViewBag
since ViewBag
maintains data on refresh, you can just call ViewData.Clear();
in your Controller method since ViewBag
uses it internally.
Upvotes: 1