简体   繁体   中英

Asp.net MVC 2: Can Ajax.ActionLink pass a parameter without it going into the url?

I have the following in partial view

<%= Ajax.ActionLink("Plunder Again", "Resources", new { controller = "Resource", parent = Model.ToJson() }, new AjaxOptions { UpdateTargetId = Model.ResourceType })%>

going to the controller method:

    public ViewResult Resources(/*ModelResource parent*/)
    {
        Debug.Assert(Request.Params["parent"]!=null);
        var jss=new System.Web.Script.Serialization.JavaScriptSerializer();
        var parent=jss.Deserialize<ModelResource>(Request.Params["parent"]);
        return View(parent.PlunderAmount);
    }

but it throws an exception because the json doesn't properly pass via url, it can't find the 'Type' parameter.

I tried simply having the ModelResource as a parameter to the Action but it came in as null

This action will also be returning a partial view if that matters in any way.

ActionLink is used to create an anchor to a URL -- the URL must be valid! In general, you don't pass a whole object to an Action because the route values have to be bound back into the model and passed to the action. You might pass an ID of object though so the Action can get the object using that key.

如果要将模型发送到控制器而不是Ajax.ActionLink,请使用Ajax.BeginForm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM