MVC Redirect to View from jQuery with parameters

前端 未结 4 1935
谎友^
谎友^ 2021-02-05 14:48

I have seen some posts relating to this but can\'t seem to get it to work. With the redirect I get a \'resource cannot be found error\'.

I am trying to redirect to a De

相关标签:
4条回答
  • 2021-02-05 14:57

    This would also work I believe:

    $('#results').on('click', '.item', function () {
                var NestId = $(this).data('id');
                var url = '@Html.Raw(Url.Action("Artists", new { NestId = @NestId }))';
                window.location.href = url; 
            })
    
    0 讨论(0)
  • 2021-02-05 15:04

    If your click handler is successfully called then this should work:

    $('#results').on('click', '.item', function () {
                var NestId = $(this).data('id');
                var url = "/Artists/Details?NestId=" + NestId; 
                window.location.href = url; 
            })
    

    EDIT: In this particular case given that the action method parameter is a string which is nullable, then if NestId == null, won't cause any exception at all, given that the ModelBinder won't complain about it.

    0 讨论(0)
  • 2021-02-05 15:07

    i used to do like this

    inside view

    <script type="text/javascript">
    
    //will replace the '_transactionIds_' and '_payeeId_' 
    var _addInvoiceUrl = '@(Html.Raw( Url.Action("PayableInvoiceMainEditor", "Payables", new { warehouseTransactionIds ="_transactionIds_",payeeId = "_payeeId_", payeeType="Vendor" })))';
    

    on javascript file

    var url = _addInvoiceUrl.replace('_transactionIds_', warehouseTransactionIds).replace('_payeeId_', payeeId);
            window.location.href = url;
    

    in this way i can able to pass the parameter values on demand..

    by using @Html.Raw, url will not get amp; for parameters

    0 讨论(0)
  • 2021-02-05 15:15

    redirect with query string

     $('#results').on('click', '.item', function () {
                        var NestId = $(this).data('id');
                       // var url = '@Url.Action("Details", "Artists",new { NestId = '+NestId+'  })'; 
                        var url = '@ Url.Content("~/Artists/Details?NestId =' + NestId + '")'
                        window.location.href = url; 
                    })
    
    0 讨论(0)
提交回复
热议问题