简体   繁体   English

通过Ajax MVC从jQuery对话框获取数据

[英]Getting data from jquery dialog via ajax mvc

have done a lot of searching, and I am not sure why this is not working. 已经做了很多搜索,但我不确定为什么这不起作用。 I have a jquery dialog in which I am displaying a partial view. 我有一个jquery对话框,其中显示了局部视图。 When I pass the data back to the controller, it shows up with a blank model. 当我将数据传递回控制器时,它显示为空白模型。

Controller: 控制器:

    public ActionResult AddIngredient()
    {
        return PartialView();
    }

    public JsonResult AddIngredientJson(Ingredient model)
    {
        Ingredient newIngredient = model;

        return Json(null);
    }

Partial View: 部分视图:

     <form id="AddIngredientForm" class="AddIngredientForm">
         <div class="logincontent">
              <label>Name:</label>
              @Html.TextBoxFor(x => x.Name, new { @class = "logintextbox" })
         </div>
         <div class="logincontent">
              <label>Category:</label>
              @Html.EnumDropDownListFor(x => x.Category, new { @class = "logintextbox" })
         </div>
     </form>

Script: 脚本:

$(document).ready(function () {
    function addIngredient() {
        $.ajax({
            url: "AddIngredientJson",
            Data: $('#AddIngredientForm').serialize(),
            Type: "POST"
        });
    }

    $(function () {
        $('#modalDialog').dialog({
            autoOpen: false,
            width: 400,
            resizable: false,
            modal: true,
            buttons: {
                "Save": function () {
                    addIngredient();
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });


        $('#openDialog').click(function () {
            $('#modalDialog').load("@Url.Action("AddIngredient")", function () {
                $(this).dialog('open');
            });
            return false;
        });

    });
});

I have tried hardcoding in data into the script, and that does not get passed either. 我尝试将数据硬编码到脚本中,但也没有通过。

Thanks for the help. 谢谢您的帮助。

javascript is case sensitive so you need to be more careful when using wordcase on object properties that should be lowercase javascript区分大小写,因此在对小写的对象属性使用单词大小写时,您需要格外小心

$.ajax({
    url: "AddIngredientJson",
    Data: $('#AddIngredientForm').serialize(),
    Type: "POST"
});

should be 应该

$.ajax({
    url: "AddIngredientJson",
    data: $('#AddIngredientForm').serialize(),
    type: "POST"
});

Also it's not a good idea to use ajax without success and error handlers. 同样,在没有成功和错误处理程序的情况下使用ajax也不是一个好主意。 Your request could fail and user would never know 您的请求可能会失败,并且用户永远不会知道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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