c# - 数据未从 View (ajax)发送到 Controller (c#)

标签 c# jquery asp.net ajax asp.net-mvc

我是 AJAX 新手,我不明白为什么我的数据没有发送到 Controller 。 因此,在我的 View 上,我有两个输入表单和一个按钮:

HTML:

<input type="text" name="AddName">
<input type="text" name="AddEmail"> 
<button class="btn btn-mini btn-primary" type="button" name="add_btn" onclick="DbAdd()">Add</button>

我需要在单击“add_btn”按钮后从这两个输入中获取数据并将它们发送到 Controller 。

JavaScript:

<script type="text/javascript">
    function DbAdd()
    {
        // Get some values from elements on the page:
        var $form = $(this),
            addedName = $form.find("input[name='AddName']").val(),
            addedEmail = $form.find("input[name='AddEmail']").val();

        $("#UserTable").html("<div>Please Wait...</div>");
        $.ajax(
        {
            type: "POST",
            url: "Save",
            data:
                {
                    name: addedName, email: addedEmail,
                },
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: OnSuccess,
            error: OnError
        });
}

这是我的 Controller 的“保存”方法(我需要将从 ajax 获取的数据保存到我的数据库):

[HttpPost]
public ActionResult Save(User userinfo)
{
    string message = "";

    using (var uc = new UserContext())
    {
        uc.UserList.Add(userinfo);
        uc.SaveChanges();
        message = "Successfully Saved!";
    }
    
    if (Request.IsAjaxRequest())
    {
        return new JsonResult { Data = message };
    }
    else
    {
        ViewBag.Message = message;
        return View(userinfo);
    }
} 

问题是,当我在 Controller 的方法中设置断点时,我不接收来自 ajax 的数据,仅接收 null 的数据。所以我向数据库添加一条空记录。 有什么建议吗?

最佳答案

看起来像 $form.find("input[name='AddName']").val()$form.find("input[name='AddEmail'] ").val() 都返回 null。您应该使用 $("input[name='AddName']").val()$("input[name='AddEmail']").val() 代替。将 DbAdd() 的定义更改为以下

<script type="text/javascript">
    function DbAdd()
    {
        // Get some values from elements on the page:
        var addedName = $("input[name='AddName']").val(),
            addedEmail = $("input[name='AddEmail']").val();

        var user = { Name: addedName, Email: addedEmail };

        $("#UserTable").html("<div>Please Wait...</div>");
        $.ajax(
        {
            type: "POST",
            url: "Save",
            data: JSON.stringify({ userinfo: user }),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: OnSuccess,
            error: OnError
        });
    }
</script>

关于c# - 数据未从 View (ajax)发送到 Controller (c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25961807/

相关文章:

c# - Docker容器中的.net核心控制台应用程序引发System.ObjectDisposedException

c# - 将 MySQL 列与方法中的参数变量进行比较

javascript - 如果元素发生变化,如何向每个元素添加或删除类

javascript - 对 API POST 使用 .done 和 .fail

c# - 无法在 Xamarin.Android 中使用 Xamarin.Mobile 组件保存联系人

c# - 如何将多个 list 文件嵌入/合并到 C# 应用程序中?

javascript - Laravel 5 Eloquent 对象和 ajax

javascript - ListView 更新,jQueryMoble

html - 如何在 RadioButtonList VB 中对齐动态列表项

asp.net - 预计保持事件状态的连接已被 asp.net 中的服务器关闭