I am submitting a form and would like that, upon validation, my Action method will return a javascript file that lives in Scripts folder. I have the file's path

<script src="~/Scripts/MyScript.js"></script>

and that is what I have tried (which obviously didn't work)

[HttpPost]
        public ActionResult AddPerson(Person person)
        {            
            if (ModelState.IsValid)
            {

                return Content("<script src='~/Scripts/MyScript.js'></script>");
            }
            return View(person);
        }

How could I do such thing?

Thanks!

1 Answers

1
Hien Nguyen On Best Solutions

You need remove ~ and change to return Content("<script src='/Scripts/MyScript.js'></script>");

I tried to reproduce it can run script in script file.