1

Is it possible to run jQuery from a MVC C# controller?

if (ModelState.IsValid){
 //lots of C# asp.net code here

 jqueryFunctionName();

}

It's important that there is a check somehow before the jQuery runs

Perhaps i can do it straight from jQuery itself, but i'm not sure how to check it?

The idea is Form -> Submit -> SOMETHING checks if valid -> if yes then wait graphic -> 2 seconds pauze -> redirect to "thanks.cshtml"

4
  • 2
    What would it do? You are confusing backend and frontend code. C# runs on the server, JavaScript runs in the browser. Commented Oct 30, 2014 at 10:26
  • I want my return RedirectToAction("Thanks"); to delay 2 seconds, so i thought i'd execute jQuery then return to that page with jquery instead Commented Oct 30, 2014 at 10:30
  • You can't do that. You can use something like Thread.Sleep(2000) before you do RedirectToAction in controller. Or you can use javascript on client via SetTimeout() to achieve same thing. But you can't use it like you wrote. Commented Oct 30, 2014 at 10:38
  • Well, that works, awesome.. only problem now is.. where do i insert my wait graphic? I wanted to check if (ModelState.IsValid) THEN insert the wait graphic, wait for 2 seconds, then RedirectToAction Commented Oct 30, 2014 at 10:48

2 Answers 2

2

In your controller's action:

var vm = new YourViewModelTypeWithPropertyIsValid();
vm.IsValid = ModelState.IsValid;
return View(vm);

In your view:

@model YourViewModelTypeWithPropertyIsValid

<script type="text/javascript">

    var isModelValid = @Model.IsValid ? 'true' : 'false';
    $( document ).ready(function() {
        // Any JS code here
        // ...

        if (isModelValid) {
            setTimeout(
                function () {
                    location.assign('/redirect_path_after_2s_delay');
                },
                2000);
            );
        }
    });
<script>

I prefer typed views.

If you use untyped views, use the code below.

In your controller's action:

ViewData["IsModelValid"] = ModelState.IsValid ? "true" : "false";
return View();

In your view:

<script type="text/javascript">
    var isModelValid = @ViewData["IsModelValid"];
    $( document ).ready(function() {
        // Any JS code here
        // ...

        if (isModelValid) {
            // See the code above.
        }
    });
<script>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes I know, but this way it does not check if the form is validated! If i run this, it will simply redirect even though the form comes up with errors saying "field is required"
@SNT check another solution. You can transfer the flag using viewmodel class with typed view.
i tried figuring out your code but i have no idea what to change your example to [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Person person) { } if (ModelState.IsValid) { bla bla }
@SNT perhaps you're using an untyped view, so you can pass the flag via ViewData or ViewBag (check this post: codeproject.com/Articles/786604/…).
i put that "ViewData..." into my [HttpPost]public ActionResult Create(Person person), but it still says that its undefined.. i really appreciate your help but i still cant figure it out
0

If the code runs in the view then you can try something like this. is a special asp tag. If the should indeed run in controller, than it is not possible to run JS code there.

if (ModelState.IsValid){
 //lots of C# asp.net code here

 <text>
  <script>
  jqueryFunctionName();
  </script>
 </text>
}

1 Comment

text is not enough, it's need to put in script tag to correctly executed in browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.