JavaScript MVC4 passing all form data (model) from javascript to controller — is it possible?

The reply at

MVC3 Pass Model view to controller using javascript

implies that this was impossible, at least for MVC 3.

I was wondering if there is any way in MVC 4 to quickly pass the entire form contents (represented by the model) from the .cshtml (razor) view to the controller in JavaScript.

For example, if I select a dropdown, I may want to return all fields from a form to the controller which will take appropriate action.

Obviously, for large forms it is undesirable to have to do this element-by-element

Answer:1

Basically, you can do it calling an AJAX POST:

JS (using jQuery):

$('form').on('submit', function (event) {
    // Stop the default submission
    event.preventDefault();

    // Serialize (JSON) the form's contents and post it to your action
    $.post('YourAction', $(this).serialize(), function (data) {
        // If you want to do something after the post
    });
});

Controller Action:

public ActionResult YourAction(string JSONmodel)
{
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

    MyModel model = serializer.Deserialize(JSONmodel, typeof(MyModel));

    // Now you can do whatever you want with your model
}

UPDATE

For more complex objects, you can use a third part solution for serialization/deserialization. It's well documented and broaden used:

Json.NET: http://json.codeplex.com/

Answer:2

I'm looking to move a div "#menu" to the side when it's clicked on. I'm using this code <script> $("#menu").click(function(){ $("this").animate({ marginLeft: "+=...

I'm looking to move a div "#menu" to the side when it's clicked on. I'm using this code <script> $("#menu").click(function(){ $("this").animate({ marginLeft: "+=...

  1. moving animations
  2. moving animations for powerpoint free download
  3. moving animations for powerpoint
  4. moving animation css
  5. moving animation images
  6. moving animation wallpaper
  7. moving animation pictures
  8. moving animations gif
  9. moving animation app
  10. moving animations for powerpoint free
  11. moving animation wallpaper for desktop
  12. moving animation background
  13. moving animation css codepen
  14. moving animation android
  15. moving animation in ppt
  16. moving animation sketchup
  17. moving animation maker
  18. moving animation photoshop
  19. moving animation software
  20. moving animations on instagram

I'm trying to create a simple meteor project based on the leaderboard example in their site. I want to add a small icon next to the score of each player. For doing so I created a folder named public ...

I'm trying to create a simple meteor project based on the leaderboard example in their site. I want to add a small icon next to the score of each player. For doing so I created a folder named public ...

  1. background image doesn't fill screen
  2. background image doesn't show css
  3. background image doesn't move when scrolling
  4. background image doesn't show
  5. background image doesn't work
  6. background image doesn't fill div
  7. background image doesn't show html
  8. background image doesn't work css
  9. background image doesn't display
  10. background image doesn't show blender
  11. background image doesn't scroll
  12. background image doesn't load
  13. background image doesn't show in safari
  14. background image doesn't show android
  15. background image doesn't fit
  16. background image doesn't show in firefox
  17. background image doesn't
  18. background-image url doesn't work
  19. css background image doesn't work
  20. html background image doesn't work

I'm very new to JavaScript and jQuery and I'm having trouble with a bit of code. HTML: <div class="toggle" style="display: block; width: 200px; height: 200px; background-color: red;">test</...

I'm very new to JavaScript and jQuery and I'm having trouble with a bit of code. HTML: <div class="toggle" style="display: block; width: 200px; height: 200px; background-color: red;">test</...

  1. access this inside promise
  2. access this inside arrow function
  3. access this inside function javascript
  4. access this inside settimeout
  5. access this inside foreach
  6. access this inside callback
  7. access this inside function
  8. access this inside map
  9. access this inside getderivedstatefromprops
  10. access this inside event
  11. access this inside event javascript
  12. typescript access this inside function
  13. angular access this inside subscribe
  14. vue access this inside function
  15. jquery access this inside function
  16. javascript access this inside callback
  17. react access this inside map
  18. javascript access this inside map
  19. angular access this inside function
  20. access to this inside prototype

In order to build the final JS source for a web app, I am compiling JavaScript templates, CoffeScript sources and vanilla JS vendor scripts. A final task, dependent on the former three, would then ...

In order to build the final JS source for a web app, I am compiling JavaScript templates, CoffeScript sources and vanilla JS vendor scripts. A final task, dependent on the former three, would then ...

  1. gulp dependent tasks order
  2. gulp 4 dependent tasks