Url.Action Not figuring out the actual url

2019-06-13 20:20发布

问题:

I am trying to use Url.Action to generate the correct HTTP URL based on a controller action like this : $.post('@Html.Raw(Url.Action("Delete", new { id = "1" }))')

However, it is not working as expected . The actual url fired (got this from dev tools) is http://localhost:60223/CordBlood/@Html.Raw(Url.Action(%22Delete%22,%20new%20%7B%20id%20=%20%224%22%20%7D))

Whereas I want something like this : http://localhost:60223/CordBlood/Delete/1

What am I doing wrong here?

回答1:

I think you are trying to achieve something similar to this

<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#dropdown").change(function () {
                var id = $("#dropdown").val();

                if (id == "")
                { id = 0; }
                var dataToSend = {
                    Id: id

                };
                RedirectToPage(id);
            });
            function RedirectToPage(id) {
                var url = '@Url.Action("Delete", "yourController", new { Id = "__id__" })';

                window.location.href = url.replace('__id__', id);
            }
        });

    </script>

Hope this will give you some idea