0

This my controller,which takes date:

public ActionResult ViewLevel(string datepicker)
        {
            @ViewBag.DatePicker = datepicker;
            return View();
        }

I have View:

@model Car

@{
    var datepicker = Convert.ToString(ViewBag.DatePicker);
}
<div class = "Conteiner"
@foreach (Car car in Model)
        {
<p data-carnumber="@car.Number"> @car.Number<p>
<button class="buyCar">Buy</button>
}
</div>

<script type="text/javascript">
    $('.Conteiner').on("click", ".buyCar", function () {
        var number = $(this).data("carnumber");
        window.location.href = '@Html.Raw(Url.Action("Form", "Buy", new {datepicker,number}))';
    });
</script>

datepicker I need to pass too. Why not see number in new {datepicker,number} .number is red

andrey1567
  • 97
  • 1
  • 13
  • 1
    you can't pass a javascript variable into asp code...they run in different environments and at different times. That asp code is run on server long before the browser ever sees it – charlietfl May 06 '15 at 16:08
  • What values are you expecting to appear instead of `datepicker` and `number`? In your current code, these would be generated as the page is processed from the server, and wouldn't hold the details of the JavaScript variables. – Zhaph - Ben Duguid May 06 '15 at 16:10

3 Answers3

1

This is one reason I hate to see Razor injected into Javascript. It confuses the whole client/server separation (and it does not allow the scripts to be in separate JS files where debugging JS will actually work in Visual Studio!).

You need to construct the parameter part of the URL at run-time on the client side, using the selected values:

e.g.

 var staticUrl = "@Html.Raw(Url.Action("Form", "Buy", new { datepicker=datepicker}))"
 $('.Conteiner').on("click", ".buyCar", function () {
      var number = $(this).prev().data("carnumber");
    window.location.href = staticUrl + "&number=" + number;
});

Here I inject only the main part of the action URL and leave the parameters to be added at click time.

Normally I inject only the site root URL into a global var with something like this in the layout file:

<script>
    window.siteRoot = @Url.Content("~/");
</script>

and use that URL in my JS/jQuery everywhere the site root is needed.

 $('.Conteiner').on("click", ".buyCar", function () {
      var number = $(this).prev().data("carnumber");
    window.location.href = siteRoot + "buy/form?datapicker=" + datepicker + "&number=" + number;
});
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
0

First, you have a syntax error:

Correct would be new {parametername1=value1, parametername2=value2}.

Seconds (as comments pointed out) you can't use a javascript variable (client/browser side) in an @Html Method (serverside). One trick I used to solve this problem is to use placeholders in the @Html.Action like new {date="###THEDATE###", number="###THENUMBER###"} and with the corresponding string that @Html returns (and which is sent to the browser and comes to rest in a javascript variable) on client side the placeholder are replaced with javascript using the string replace function.

Holger Thiemann
  • 1,042
  • 7
  • 17
0

It looks as though your problem is that you are trying to use a javascript variable in a razot Html helper parameter.

You cannot do this because razor syntax is executed on the server and javascript is executed client side.

Please see this answer

Community
  • 1
  • 1
chxzy
  • 489
  • 4
  • 13