2

I would like to be able to pass the data from a text box on a page to tan Action using ActionLink.

I specify the actionLink as follows in my view

@Ajax.ActionLink(
    "UTC",
    "GetTime",
    { zone="BST" },
    new AjaxOptions {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "TargetElementID",
        OnBegin = "GetTime_OnBegin",
    }
)

Since I cannot see any way to specify this in the helper I thought to implement the OnBegin event and do it there. I noted that OnBegin receives XMLHttpRequest, containing details of the request that will be performed and an object, whos purpose escapes me, but contains a url property object.url as well as object.context.url, So I thought to add the property I need to pass to my action to the url, thinking it would resolve to the same-named parameter of the action.

function GetTime_OnBegin(XMLHttpRequest, object) {
    object.context.url = object.context.url + "&text=" + $('TextBoxId').val();
}

My controller action

public string GetTime(string zone, string text) {
        DateTime time = DateTime.UtcNow;
        return string.Format(
            "{0:h:mm:ss tt}({1},{2})",
            time, zone.ToUpper(),text);
}

However text is not being passed and is always null.

Is there a more elegant way to do this ?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
ricardo
  • 1,452
  • 1
  • 22
  • 24
  • Have you thought about using Ajax.Beginform? [See MSDN](http://msdn.microsoft.com/en-us/library/system.web.mvc.ajax.ajaxextensions.beginform%28v=vs.108%29.aspx) , [This might also help](http://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor) – christophersw Aug 28 '12 at 20:23

0 Answers0