C# 将数据从布局发送到控制器Asp.Net MVC

C# 将数据从布局发送到控制器Asp.Net MVC,c#,asp.net,asp.net-mvc,controller,C#,Asp.net,Asp.net Mvc,Controller,我在布局视图的导航栏上有一个开关按钮,当我单击该按钮时,它会更改值并将其发送给控制器: <input type="button" id="switchbutton" value="Weekend" style="color:blue" onclick="toggle(this)" > <script type="text/javascript"> function toggle(button) { switch (button.value) {

我在布局视图的导航栏上有一个开关按钮,当我单击该按钮时,它会更改值并将其发送给控制器:

<input type="button" id="switchbutton" value="Weekend" style="color:blue" onclick="toggle(this)" >

<script type="text/javascript">
    function toggle(button) {
        switch (button.value) {
            case "Weekend":
                button.value = "Week";
                break;
            case "Week":
                button.value = "Weekend";
                break;
        }
        $.ajax({
            type: "POST",
            url: "/Home/AjaxMethod",
            data: '{param: "' + $(this).val() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
                alert("Return Value: " + response);
            },
            failure: function(response) {
                alert(response.responseText);
            },
            error: function(response) {
                alert(response.responseText);
            }
        });
    }
</script>

问题是,我确实单击了按钮,但没有看到任何更改,似乎值只是第一次到达控制器。有人能帮我吗?

不是那样的。不能在服务器端保存多个不同请求的值。也许您可以使用
Session
来执行它,但是为什么不发布数据
ModelsUpdate
操作而不是
AjaxMethod
。在您的情况下,我无法理解
AjaxMethod
的用途

    public JsonResult ModelsUpdate(string SwitchVal)
    {
        if (SwitchVal == "Weekend")
        {
            resultminDate = CalculateminDate(minDate, todayDay);
        }
        else
        {
            resultminDate = CalculateminDateWeek(minDate, todayDay);
        }
        return Json(resultminDate);
    }
Ajax
调用看起来像

$.ajax({
    type: "POST",
    url: "/Home/ModelsUpdate",
    data: '{SwitchVal: "' + $(this).val() + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        alert("Return Value: " + response);
    },
    failure: function(response) {
        alert(response.responseText);
    },
    error: function(response) {
        alert(response.responseText);
    }
});

private
SwitchVal
变量不会在两次http调用之间保存数据。现在发生了什么?它正在进行ajax调用吗?您是否收到脚本错误?“我没有看到任何更改”预期的更改是什么?@Shyju SwitchVal变量在两个函数之外,为什么它不会保留该值?@rainman该值用于在函数或其他函数上运行的if语句,这些函数返回不同的数据,但我总是看到相同的值,我这样做了,没有出现错误,但我的问题仍然是一样的,当我单击按钮时,视图数据不会更改您正在执行的操作,只是在请求成功完成时发出警告消息。您希望得到什么?不,我在If语句中返回了值,该语句根据返回的值运行函数。请检查代码。我现在明白你说的了,我需要用新数据刷新我的视图。我可以用新数据刷新我的索引视图吗?
$.ajax({
    type: "POST",
    url: "/Home/ModelsUpdate",
    data: '{SwitchVal: "' + $(this).val() + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        alert("Return Value: " + response);
    },
    failure: function(response) {
        alert(response.responseText);
    },
    error: function(response) {
        alert(response.responseText);
    }
});