0

I have this function that I would like to be able to make changes to a static class.

$("#DDLRegion").change(function () {

     var selectedItem = $(this).val();
     ViewBag.Region = selectedItem;
     @RegionSwitch.setRegion(ViewBag.Region);

     switch (selectedItem) {
        case 'Place':
           window.location.href = '@Url.Action("Index", "Place")'
           break;
        case 'Thing' :
           window.location.href = '@Url.Action("Index", "Thing")'
           break;
        case 'Region' :
           window.location.href = '@Url.Action("Index", "Region")'
           break;
        default:
           window.location.href = '@Url.Action("Index", "Place")'
           alert("Blarg");
           break;
     }
  });

Is there a way to be able to access a C# static class from within (what I believe to be) a jQuery script? I'm new to MVC and web development in general. Any advice is also welcome on becoming a better dev.

@RegionSwitch._currentRegion = selectedItem;

The above does not work either and appears in Chrome's JavaScript console as = selectedItem;

3
  • possible duplicate: stackoverflow.com/questions/4161874/… Commented Apr 28, 2015 at 20:11
  • 3
    You will need to post to your controller to change the server side variables. Google jQuery post for more info. Commented Apr 28, 2015 at 20:13
  • 4
    You're trying to change a serverside object from the client, that won't work. The client javascript is unaware of any server side objects, so you would have to post your values to the server, for instance using an Ajax call. Commented Apr 28, 2015 at 20:27

2 Answers 2

0

As @BillCheng and @Stephen said, what you are trying to do is to do a post request from jQuery.

This is how you implement a post request (Ajax) on jQuery.

On the ASP.NET MVC side, Actions work the same regardless of the input being Ajax or not. You just have to implement your Action parameters in such a way that they will match your post parameters. Here's a good article about model binding.

Sign up to request clarification or add additional context in comments.

1 Comment

So I threw this here post request at it, but I can't seem to get it to work. Gives a 500 error (super helpful). $.ajax({ url: '/region/changeRegion', type: 'POST', data: selectedItem, dataType: 'String', success: function (data) { alert(data); }, error: function () { alert('Nope'); } });
0

OK, so I've solved my problem after looking around at POST requests. Thanks @BillCheng, @Stephen and @andrerpena for the advice. I posted my data to a controller which then did all the cool C# things I needed.

Ajax request inside javascript change function

     $.ajax({
        url: '/region/changeRegion',
        type: 'POST',
        data: { "selectedRegion": $(this).val() },
        dataType: 'text',
        success: function (data) {
           alert(data);
        },
        error: function () {
           alert('Nope');
        }
     });

And a controller action to handle the string (js calls it text -_-)

  [HttpPost]
  public void ChangeRegion(string selectedRegion)
  {
     RegionSwitch._currentRegion = selectedRegion;
  }

For anyone running into this problem in the future, the expected parameter name needs to match the one from the ajax request.

data: { "**selectedRegion**" : $(this).val() },
public void ChnageRegion(string **selectedRegion**)

Also don't forget to match up the url to the correct controller and action.

Comments

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.