0

I'm working on a MVC3 web-application and encounter a problem when passing values from my model back to a Action using AJAX.

When I

1) browse to this view 2) alter the Quantity textbox 3) hit save

I get the same value for "Quantity" as I get when I pass the Model into the View. I would expect the Model to be synchronous with the Textfields but apperently they are not. I need either the Model to be synchronized - or some other way of getting the new "Quantity" value into the ActionLink.

-- SOLUTION :

Basically the reason why my FormCollection did not update was because I was using the Html.BeginForm function, and calling Ajax.ActionLink. These do not work well together. When changing to Ajax.BeginForm I successfully was able to return the updated FormCollection.

(I cannot paste code here now because it's been refactored alot and is not recognizable)

AndersLindas
  • 137
  • 1
  • 9

1 Answers1

1

To update a model you will need to issue a POST. Then you will need to adorn your Save method with the HttpPost attribute and accept your model as the first argument. The framework will pass the argument to you.

Here is a supporting article on this forum.

ASP.NET MVC [HttpPost] action accepts single object, spits back validation errors to ViewPage<CustomViewModel>

EDIT

Added a modified link more directly related to Ajax ActionLink -- MVC Ajax.ActionLink doesn't find POST method

Community
  • 1
  • 1
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Ok that makes sense. However I get an empty Order object back from the View now. If I try setting the [Bind(Prefix="Order")] like in the link you posted i get a null reference back. What is the Bind suppose to do anyways? – AndersLindas Feb 17 '12 at 14:44
  • You probably don't need that and can pull it. Here is a post explaining the Bind -- specifically what Prefix means -- let me know if that helps! [link]http://stackoverflow.com/questions/1317523/how-to-use-bind-prefix – Mike Perrenoud Feb 17 '12 at 14:48
  • Seems to me like the main problem is that the model is not synchronized with the values im typing. This is not a Html.ActionLink, but an Ajax.ActionLink. I dont know if that makes any difference. When I try to pass orderNumber and quantity explicitly as a parameter I get the original value and not the value I've modified it to. – AndersLindas Feb 17 '12 at 14:58
  • Check out this post [link]http://stackoverflow.com/questions/3036915/mvc-ajax-actionlink-doesnt-find-post-method – Mike Perrenoud Feb 17 '12 at 15:56
  • Ill check that out on monday and get back to you. Thanks:) – AndersLindas Feb 17 '12 at 19:51
  • That did not alone fix the problem. I also needed to use Ajax.BeginForm and not Html.BeginForm. Also removed passing arguments in the Ajax.ActionLink, but instead letting it pass the FormCollection. Problem is not solved. Thanks for the help :) – AndersLindas Feb 20 '12 at 09:40
  • @AndersLindas First, can you update the question with the new code that's working? Second, did you mean to say that the problem is "now" solved? I just want to make sure you did get it resolved. Thanks! – Mike Perrenoud Feb 20 '12 at 12:53
  • Yeah I mean "now" ofcourse :) Thanks for your help. I was not able to insert the working code but I hope others can get help from the text I wrote. – AndersLindas Feb 22 '12 at 12:08
  • Thanks! I'm glad I was able to help move you the right direction at least! – Mike Perrenoud Feb 22 '12 at 13:09