使用输入参数通过CSHTML页面调用void方法

时间:2019-08-23 10:30:59

标签: c# razor

我正在构建一个简单的MVC应用程序。在我的CSHTML文件中,我正在构建以下代码:

<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>

这是void方法的代码:

public void saveRROriginal(String idSlott)
        {}

警告消息可正确打印idSlot的值,但在无效方法中idSlott = null。

1 个答案:

答案 0 :(得分:1)

您的json格式无效。应该是:

{ "idSlott": "idSlot" }

还要在您的请求中添加以下行:

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