0

I'm using this on a button's onclick to trigger a ActionResult

function update(element) {
            var form = $(element).closest('form');
            form.attr('action', "@Url.Action("CompanyUpdate")");
            form.attr('data-ajax-method', "POST");
            //form.attr('data-ajax-update', "#ajax-target");
            form.submit();
        }

But I'm having problems with sending a parameter, an url parameter to ActionResult.

my url seems like this now

....Admin/Registration/AdminCompanySettings?Id=98

and I can succesfully take the id parameter out of it with another javascript function. I'm stuck at sending it to ActionResult. How can I manage it?

I recently started all these and don't know if JS is right way to go

EDIT:

My ActionResult is like this

public ActionResult CompanyUserAdd(Ebelge.ViewModels.AdminCompanySettingViewModel CompanyInfo)
        {
           //stuff inside
        }

I was planning to get the url parameter inside it via

int CompanyId = Convert.ToInt32(Request["Id"]);

EDIT 2:

I'm trying something like this now. I put the js function which I get the parameters from url inside my update function but it gives "Id doesn't exist in current content" error.

function update(element) {

             var getUrlParameter = function getUrlParameter(sParam) {
                 var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                     sURLVariables = sPageURL.split('&'),
                     sParameterName,
                     i;

                 for (i = 0; i < sURLVariables.length; i++) {
                     sParameterName = sURLVariables[i].split('=');

                     if (sParameterName[0] === sParam) {
                         return sParameterName[1] === undefined ? true : sParameterName[1];
                     }
                 }
             };

             var Id = getUrlParameter('Id');


            var form = $(element).closest('form');
            form.attr('action', "@Url.Action("CompanyUpdate", new RouteValueDictionary(new { Id = Id}) )");
            form.attr('data-ajax-method', "POST");
            //form.attr('data-ajax-update', "#ajax-target");
            form.submit();
        }
Ege Bayrak
  • 1,139
  • 3
  • 20
  • 49
  • you should stick to using POST or GET, not both... – dandavis Apr 04 '16 at 18:52
  • As I said I'm very new to this. I don't know much about it. – Ege Bayrak Apr 04 '16 at 18:53
  • post at least the signature of your controller action – Golois Mouelet Apr 04 '16 at 19:01
  • Possible duplicate of [Parse URL with Javascript](http://stackoverflow.com/questions/4140324/parse-url-with-javascript) – Shanimal Apr 04 '16 at 19:10
  • I 'm wondering if I can manage it by doing something like form.attr('action', "@Url.Action("CompanyUpdate")/"+Id+"); – Ege Bayrak Apr 04 '16 at 19:13
  • `@Url.Action()` is razor code which is evaluated on the server before its sent to the view. `id` is a client side javascript variable which does not even exist at that point (its not in scope). You need to build your url as per you last comment. –  Apr 04 '16 at 22:07

0 Answers0