简体   繁体   中英

Call void method by CSHTML page with input parameter

I'm building a simple MVC application. In my CSHTML file I'm build this code:

<script type="text/javascript">
        //modify as needed to make it pass in what you need.
    function GeneratePdf() {
        alert(idSlot);
            $.ajax({
                url: "@Url.Action("saveRROriginal", "Martinenko")",
                data: { idSlott:idSlot },
                cache: false,
                contentType: false,
                processData: false,
                type: "POST",
                success: function (data) {
                    //TODO: Add whatever if you want to pass a notification back
                    alert("success");
                },
                error: function (error) {
                    //TODO: Add some code here for error handling or notifications
                    alert("no success");
                }
            });
        }
</script>

<div class="col-md-12" style="width:100%;height:100%;margin-top:5px;">
            <button onclick=GeneratePdf()>
               Salva RR</button>
        </div>

This is the code of void method:

public void saveRROriginal(String idSlott)
        {}

Alert message print correctly the value of idSlot but in void method idSlott = null.

Your json format is not valid. It should be:

{ "idSlott": "idSlot" }

Also add following lines to your request:

contentType: "application/json; charset=utf-8",
dataType: "json",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM