-1

I have buttonOnclick,which takes 2 parameters

$('.Conteiner').on("click", ".Button", function () {
        window.location.href = '@Url.Action("Form", "State", new {numberNights, datepicker})';
    });

I need to pass parameters from @Url.Action to controller

My controller:

public ActionResult Form(int numberNights, string datepicker)
        {
            @ViewBag.NumberNights = numberNights;
            @ViewBag.DatePicker = datepicker;
            return View();
        }

Parameter numberNights is pass Ok, but datepicker always null. Why?

Alex Da
  • 61
  • 1
  • 2
  • 6
  • Try this `window.location.href = "State/Form?numberNights="+numberNights"+&+"datepicker=" + datepicker;` Make sure your datepicker has a value!! – Guruprasad J Rao May 04 '15 at 13:29

1 Answers1

4

First, you need to actually name the members of the anonymous object, i.e.:

new { numberOfNights = numberOfNights, datepicker = datepicker }

numberOfNights, is probably coming through by sheer accident, since it's the first parameter of the action.

Second, make sure that you're not mixing client-side and server-side code. Namely, in order for a value to be passed for datepicker, it must be a C# variable defined in your Razor code, not a JavaScript variable. The value, also, must exist before your JavaScript has a chance to run. If you need to update it dynamically upon the user changing the datepicker value, you'll need to forgo passing it in Url.Action.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444