0

I have got a question very similar to this one how to call javascript function from Controllers file in MVC? but the answer provided wasn't quite what I required.

What I want to do is this, I want to return a view but that I'd like to trigger/call a Javascript/jQuery event. For instance in my controller:

 public ActionResult List()
    {
        // do some processing
        // Call the javascript/jquery event            

        return this.View();
    }

Now the way I tried to this was to use the view bag like:

 public ActionResult List()
    {

        ViewBag.MovePanel = "yes";
        return this.View();
    }

Then in my View I've got soemething like:

<input type="hidden" name="move" id="move" value="<%: (string)ViewBag.MovePanel %>" />
<script type="text/javascript">
$('#move').change(function () {
    console.log('changed');
});

The value gets set to "yes" but the "change" event never gets triggered.

How can I achieve what I'm trying to do?

Edit: Following Darin's comment, here is what I am trying to achieve. We are trying to create a form carousel similar to an image carousel. So, what I want to do is control whether the next form is displayed.

For instance, a typical workflow would be something like this:

  1. Say I am on the first form which is a list of items. I select and item and click edit.
  2. What then happens is that the list form is hidden from view (but not closed) and the edit form is shown to the user.
  3. The user then makes some changes to the form and tries to save.
  4. If there are no validation errors, the edit form is closed and the previous list form is shown into view.

So effectively what I'm having problems with the last step i.e. getting the list form to come into view.

In the wider context say that when I submitted the form and there was an error, then I should be able to keep the "Edit" form in view, not close it and not show the previously opened "List" form.

I hope that this was a clearer explanation.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DavidS
  • 2,179
  • 4
  • 26
  • 44
  • There is something fundamentally wrong with your design if you need something like this. So in order to help you could you explain what is your scenario and what are you trying to achieve (and don't say I am trying to call a javascript event from a controller action as this doesn't make any sense). Describe your final goal. – Darin Dimitrov Jul 08 '11 at 11:41

2 Answers2

0

One thing you should take care of is, you cannot call javascript (client code) method from your server side . ( except doing pushing).

and, on your code, value of $("#move") is never change. It is as it is value as "yes". it will trigger if you change value of $("#move") than "yes".

Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61
0

Ok,

Darin was right, the design was off. So what I ended up having to do was to "ajaxify" all of my forms instead of relying on an HTML form submit. I was reticent to do that simply because it would have meant more work but after having done it, it didn't take as long as I had thought.

However the following really helped me a lot How to read modelstate errors when returned by Json?.

Community
  • 1
  • 1
DavidS
  • 2,179
  • 4
  • 26
  • 44