0

While there appears to be similar questions I do not believe I found one where somebody was passing the entire model from view to controller, the closest I found was passing a data value in the model to the controller... which is not what I want to do...

I want to pass the model object to the applicable controller action result as a parameter and do what I want to do with the data in the model object from within the controller...

code abbreviated...

in the view i am using Url.Action(string, string, object) to pass the controller method name, and the route to it as the 2nd parameter, and the entire model object as the 3rd parameter...

my third parameter is the problem, the Url.Action(string, string) over load works fine with the same values, so I don't have any problem getting to the controller action result method, with the values I am using...

view code snippet...

$('#ControllerActionResult').click(function (e) {
  window.location = '<%: Url.Action("ControllerActionResult", TheRoute, new { pModel = Model } ) %>';
});

controller code snippet...

public ActionResult ControllerActionResult(object pModel)
{
}

My error is "CS1502: The best overloaded method match for 'System.Web.Mvc.UrlHelper.Action(string, string, object)' has some invalid arguments" and the window location line is highlighted...

Am I missing something obvious?

rae1
  • 6,066
  • 4
  • 27
  • 48
Bohemian
  • 1
  • 1
  • 1
  • 1

3 Answers3

4

I don't think you can do that. Url is trying to build a url, not call the action method directly. You can pass individual key value pairs via the anonymous object, which Url can then use to build a link. Model binding can take care of putting the key value pairs into the incoming model for you.

Also, is TheRoute a string as well?

If you need a Url you don't have many alternatives. Either pass all the values to rebuild the model, or just pass a key value to retreive the model from the database again (or maybe session / viewdata / tempdata, if you're using that).

Andy
  • 8,432
  • 6
  • 38
  • 76
  • yeah, the route param actually gets passed in as Model.TheRoute which looks like the following... public static readonly RouteValueDictionary TheRoute = new RouteValueDictionary(new { controller = "Home", action = "ControllerActionResult", area = "" }); – Bohemian Jul 28 '11 at 18:12
  • TheRoute has to be a string for the overload you're calling, not a `RouteValueDictionary`. – Andy Jul 28 '11 at 18:15
  • when I use Url.Action(string, string) with the same routevaluedictionary and values and call a action result in the same controller with no parameters it works just fine... so i am pretty sure that the 3rd parameter when i use Url.Action(string, string, object) is my problem... also when the jquery click event is called I already have the data I want in the model being used by the view, I just want to pass the model to the controller and use whatever model values have data in them. If I can't pass the entire model from view to controller what are some possible alternatives? – Bohemian Jul 28 '11 at 18:27
  • I doubt your calling `Url.Action(string, string)`. Probably `(string, object)` – Andy Jul 28 '11 at 19:21
  • My bad ... Public method Action(String, RouteValueDictionary) Generates a fully qualified URL to an action method for the specified action name and route values. Public method Action(String, String, Object) Generates a fully qualified URL to an action method by using the specified action name, controller name, and route values... http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action.aspx – Bohemian Jul 28 '11 at 22:37
0

There might be several issues in your code. But I will answer what you want to know, how to send data to controller from js. You have to first send "model" in json format, possibly with jquery.ajax. For example,

$.ajax({
   url: '@Url.Action("ControllerActionResult")',
   type: 'POST',
   data: JSON.stringify(model),
   dataType: 'json',
   processData: false,
   contentType: 'application/json; charset=utf-8',
   success: OnSuccess
});

In controller, you have to have a defined class for the "model" and that should be the parameter of your controller.

[HttpPost]
public ActionResult ControllerActionResult(Model model) {
  if (Request.IsAjaxRequest()) {
   //to do
  }
}

Otherwise, MVC cannot find a right controller to call.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • thanks, will give it a go, any suggestions on feeding the model into JSON.stringify(model) ? looks like it needs to be serialized first and the model is server side while this is client side... suggestions? have not worked with json yet. – Bohemian Jul 28 '11 at 22:40
0

try with following code it works for me

Javascript:

var location ='@url,action(string ,obj)'; window.location=location;

Controller:

public ActionResult index(string obj){return view();}

hope this works for you.

sushil
  • 9
  • 1