jquery - 在 mvc 中,使用基于表单的模型绑定(bind)与 javascript 序列化时如何获得响应?

标签 jquery asp.net-mvc model-binding

我在 MVC 中创建了一个表单,最初使用 javascript 序列化和 jQuery 到 $.post() 到 Controller 方法。这工作正常,只是由于某种原因单个字段没有绑定(bind)到方法参数,所以我切换到 MVC 内置模型绑定(bind),这解决了这个问题。然而,这样做的结果是我无法回复消息。我的验证消息仍然显示正常,但“alert('saved')”之类的东西不起作用,而且我不知道如何执行其他操作,例如有条件地刷新页面等。

感谢任何帮助。

谢谢。

编辑:

jQuery 代码是:

        $.post(
        'controllerURL',
        {
            IRN: irn,
            SepId: sepId,
            JvsId: jvsId,
            From: from,
            To: to,
            Notes: notes,
            CTA: cta
        },
        function (data) {
            if(data.Success == null) {
                alert(data.Error);
                return;
            }

            alert(data.Success);
            reloadPage();
        },
        'json'
    );

Controller 方法如下所示:

    [HttpPost]
    public ActionResult UpdateJvsEntry(JVSDataEntryModel model) //(string IRN, int SepId, int? JvsId, DateTime From, DateTime? To, string Notes, int CTA)
    {
        if (model.To.HasValue && model.To.Value < model.From)
        {
            ModelState.AddModelError("Error", "To can not be prior to From.");
            //return Json(new { Error = "To can not be prior to From." });
        }

        var entry = new EmisJvs();

        if (model.JvsEntryId > 0)
        {
            entry = _jvsDataService.GetWhere(w => w.JvsId == model.JvsEntryId && w.SEPID == model.SepId).Single();
        }
        else
        {
            if (DoesJvsHaveOpenEntries(model.SepId))
            {
                ModelState.AddModelError("Error", "You must close out the current JVS entry before creating a new one.");
                //return Json(new { Error = "You must close out the current JVS entry before creating a new one." });
            }

            entry.SEPID = model.SepId;
        }

        entry.SchoolIRN = model.SchoolIRN;
        entry.From = model.From;
        entry.To = model.To;
        entry.Notes = model.Notes ?? string.Empty;
        entry.Percentage = model.CTA;
        entry.ChangedOn = DateTime.Now;
        entry.ChangedBy = _userService.CurrentUser().SepId;

        if (model.JvsEntryId == 0)
        {
            _jvsDataService.Insert(entry);
        }

        if (ModelState.IsValid)
        {
            _jvsDataService.SaveChanges(entry);
        }

        return View("JVSDataEntry", model);
    }

另一个编辑:

我最终回到了之前所做的 $.post() 解决方案。通过 native MVC 表单进行模型绑定(bind)路线与我正在使用的当前项目架构并不相符,我只是在原地踏步,所以我只是备份并应用了 KISS。促使我研究模型绑定(bind)的最初问题与页面上或 jQuery 本身的奇怪现象有关。我可以验证页面上存在 id 为“Notes”的元素,但是当我执行 $('#Notes').val() 时,即使文本确实在输入框中,我也没有返回任何内容。我最终不得不执行长格式级联样式选择器和 $('div[class=\'jvsDataEntryDiv\'] input[id=\'Notes\']').val()相反,可以工作。

谢谢杰西和其他人。

最佳答案

当您从帖子中返回 View 时,您的客户端消息不起作用。如果您想处理刷新客户端,那么您可能应该返回某种形式的 json 结果:

return new JsonResult
{
    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
    Data = new { Success = "Some success message here" }
};

编辑: 根据您的评论,我不相信您的 jQuery $.post 函数设置正确。尝试添加以下内容:

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

关于jquery - 在 mvc 中,使用基于表单的模型绑定(bind)与 javascript 序列化时如何获得响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585962/

相关文章:

asp.net-mvc - ASP.NET MVC3 无法解析 jQuery $.ajax() 发送的列表类型的 URL 参数

asp.net-mvc - 是否需要特定的 IIS 配置才能允许 .Net 4.0 ASP.Net MVC 2 Azure 应用程序正常运行?

javascript - JQuery 测试工具

jquery - 选择相同输入时禁用输入字段?

c# - 使用 Ajax 从 Controller 返回 String[]

function - 如何获取 Azure FunctionApp 绑定(bind)的返回绑定(bind)属性

c# - 绑定(bind)一个 [Serializable] 类的 ASP.Net MVC 模型

c# - 将具有对象列表的 json 绑定(bind)到模型

jquery - 如何使用 Microsoft 认知服务 - 说话人识别 API

javascript - 替换数组中的输入值时出现问题