在MVC中使用AJAX将varible传递和处理到函数

时间:2017-11-27 19:41:29

标签: c# ajax asp.net-mvc

我在将视图中的变量传递给函数时遇到了麻烦。我对MVC很新,不知道如何保存和传递信息。

@model Models.Schedule.SheduleModel
@{
    Layout = null;
}

<div>
    <div class="tableRow">
        <p>Make a schedule reminder.</p>
    </div>

    <div class="tableRow tableRowHeading">
        <div class="row" style="width: 210px">Name</div>
        <div class="row" style="width: 210px">Number</div>
    </div>

    @foreach (var shedule in Model.ScheduleList)
    {
        <div class="tableRow">
            @using (Html.BeginForm("UpdateSchedule", "Schedule", FormMethod.Post))
            {
                <div class="cell" style="width: 210px">
                    @Html.HiddenFor(model => schedule.Id)
                    @Html.TextBoxFor(model => schedule.Name, new { @class = "inputFieldText" })
                    @Html.ValidationMessageFor(model => schedule.Name)
                </div>
                <div class="cell" style="width: 210px">
                    @Html.TextBoxFor(model => agent.ContactNumber, new { @class = "inputFieldText" })
                    @Html.ValidationMessageFor(model => agent.ContactNumber)
                </div>
                <div class="cell">
                    <button name="Update" type="submit" value="Update" class="button" title="Update details">
                        <span class="text">Update</span>
                    </button>
                </div>
                <div class="cell">
                    <button class="button" type="button" onclick="deleteFromSchedule();" value="Delete">
                        <span class="text">Delete</span>
                    </button>
                </div>
            }
        </div>
    }
</div>

@Scripts.Render("~/bundles/jqueryval")

<script>
    function deleteFromSchedule() {
            $.ajax(
                {
                    type: 'POST',
                    url: urlBase + 'Schedule/UpdateSchedule/' + Id,
                    data:
                    {
                        Id: Id
                    },
                    success: function (data) {
                        console.log(data);
                    },
                    error: function () {
                        var errorMessage = 'Error occurred while sending message';
                        console.log(errorMessage);
                    }
                });
    }
    }
</script>

我正在尝试将HiddenFor中的计划ID传递给删除功能,但我尝试的所有内容都不起作用,我也很好奇如何处理后来未写入的div中文本框中获取的信息,我我想在屏幕上生成文字说

用户@ Model.Name和号码@ Model.Number将收到时间表更改通知,但我一直显示空格。我使用我正在为此信息创建的表单,语法是什么?我在计划控制器中的方法非常简单。

[HttpPost]
    public void UpdateSchedule(int Id)
    {
    ////do stuff here
    }

1 个答案:

答案 0 :(得分:0)

最简单的方法是将日程表中的id添加到内联函数调用中(使用razor),并将id param添加到javascript delete函数中:

<div class="cell">
    <button class="button" type="button" onclick="deleteFromSchedule(@schedule.Id);" value="Delete">
        <span class="text">Delete</span>
    </button>
</div>


<script>
    function deleteFromSchedule(id) {
            $.ajax(
                {
                    type: 'POST',
                    url: urlBase + 'Schedule/UpdateSchedule/' + id,
                    data:
                    {
                        Id: id
                    },
                    success: function (data) {
                        console.log(data);
                    },
                    error: function () {
                        var errorMessage = 'Error occurred while sending message';
                        console.log(errorMessage);
                    }
                });
    }
    }
</script>