Jquery 对话框上的执行连续两次未按预期正确执行

发布于 2024-12-21 17:25:09 字数 3432 浏览 2 评论 0原文

我正在尝试创建一个 Jquery 对话框,它基本上是一个接受用户输入的表单。

我没有执行常规的“保存并关闭”对话框,而是尝试执行“保存,然后执行任何操作,然后在需要时关闭”对话框,这本质上意味着在单击“保存”后视图模型将传递到控制器第一次,将返回,然后允许进一步保存。

我的带有保存按钮的对话框代码如下所示:

$(function () {
        $("#EventDialog").dialog({
            autoOpen: false, minWidth: 600, minHeight: 360, modal: true,
            buttons: {
                "Save": function () {
                    $.validator.unobtrusive.parse("#EventManage");

                    if ($("#EventManage").valid()) {
                         $.ajax({
                            url: "/Home/EventSave",
                            type: 'POST',
                            data: $("#EventManage").serialize(),
                            success: function (result) {
                                //alert("s");
                                $("#EventManage").html(result);
                                //$.validator.unobtrusive.parse("#EventManage");
                                LoadEventList();

                            }
                        });

单击对话框的保存按钮将在控制器中执行 EventSave 操作。

public ActionResult EventSave(EventManageModel model)
    {
        if (ModelState.IsValid)
        {
            Event e = new Event();
                    try
                    {
                        //get existing event
                        if (!String.IsNullOrWhiteSpace(model.EventID))
                        {
                            Guid id = default(Guid);
                            if (Guid.TryParse(model.EventID, out id))
                                e = EventService.GetRecord(id, usr);
                        }

                        e.Name = model.Event_Name;
                        e.Date = DateTime.Parse(model.Event_Date);
                        e.Sender = usr;

                        e = EventService.SaveUpdateEvent(e);

                        //just to update the model with saved data.
                        model.EventID = e.Id.ToString();
                        model.Event_Name = e.Name;
                        model.Event_Date = e.Date.ToString("dd/MM/yyyy");

                        TempData["Message"] = string.Format("Event saved successfully.");
                    }
                    catch (ApplicationException ex)
                    {
                        ModelState.AddModelError("_FORM", ex.Message);
                    }
            }
        }
        return PartialView("EventManage", model);
    }

我的模型有一个 eventid,可以跟踪正在配置的事件。

public class EventManageModel
    {
        [HiddenInput(DisplayValue = false)]
        public string EventID { get; set; }

        [Required]
        [Display(Name = "Event Name")]
        public string Event_Name { get; set; }

        [Display(Name = "Event Date [DD/MM/YYYY]")]
        [RegularExpression(@"^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$", ErrorMessage = ExceptionConstants.ERR_DATE_INVALID)]
        public string Event_Date { get; set; }
    }

目前,当我第一次单击“保存”时,传递到控制器中操作的模型是有效的,具有完整的有效数据。返回的视图是按照上面对话框的按钮执行加载的。但是,第二次保存时传入的模型为空。

这种行为应该是这样的吗?或者我的代码有什么问题吗?

编辑

发布后,我运行firebug,当我单击对话框中的任何文本框时,它似乎显示此错误(我不知道这意味着什么)

f is undefined [中断此错误] "");f.settings[g]&&f.settings[g].call(.../\s/),function(g,h){b[h]= e})});var d=

jquery....min.js(第 22 行)

有什么想法吗?

I'm trying to create a Jquery dialog which is basically a form that accepts input from the user.

Instead of doing a regular "save & close" dialog, I am trying to do a "save, and do whatever, then close when you like it" dialog, which essentially means the view model being passed to the controller after clicking save the first time, will return, and then allow for further saves.

My dialog code with the save button is like so:

$(function () {
        $("#EventDialog").dialog({
            autoOpen: false, minWidth: 600, minHeight: 360, modal: true,
            buttons: {
                "Save": function () {
                    $.validator.unobtrusive.parse("#EventManage");

                    if ($("#EventManage").valid()) {
                         $.ajax({
                            url: "/Home/EventSave",
                            type: 'POST',
                            data: $("#EventManage").serialize(),
                            success: function (result) {
                                //alert("s");
                                $("#EventManage").html(result);
                                //$.validator.unobtrusive.parse("#EventManage");
                                LoadEventList();

                            }
                        });

Clicking on the dialog's save button executes the EventSave action in the controller.

public ActionResult EventSave(EventManageModel model)
    {
        if (ModelState.IsValid)
        {
            Event e = new Event();
                    try
                    {
                        //get existing event
                        if (!String.IsNullOrWhiteSpace(model.EventID))
                        {
                            Guid id = default(Guid);
                            if (Guid.TryParse(model.EventID, out id))
                                e = EventService.GetRecord(id, usr);
                        }

                        e.Name = model.Event_Name;
                        e.Date = DateTime.Parse(model.Event_Date);
                        e.Sender = usr;

                        e = EventService.SaveUpdateEvent(e);

                        //just to update the model with saved data.
                        model.EventID = e.Id.ToString();
                        model.Event_Name = e.Name;
                        model.Event_Date = e.Date.ToString("dd/MM/yyyy");

                        TempData["Message"] = string.Format("Event saved successfully.");
                    }
                    catch (ApplicationException ex)
                    {
                        ModelState.AddModelError("_FORM", ex.Message);
                    }
            }
        }
        return PartialView("EventManage", model);
    }

My model has a eventid which tracks down which event is being configured.

public class EventManageModel
    {
        [HiddenInput(DisplayValue = false)]
        public string EventID { get; set; }

        [Required]
        [Display(Name = "Event Name")]
        public string Event_Name { get; set; }

        [Display(Name = "Event Date [DD/MM/YYYY]")]
        [RegularExpression(@"^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$", ErrorMessage = ExceptionConstants.ERR_DATE_INVALID)]
        public string Event_Date { get; set; }
    }

At the moment, when I click save the first time, the model passed into the action in the controller is valid, with full valid data. the returned view is loaded as per above dialog's button execution. however saving it the second time the model being passed in is null.

Is this behavior supposed to be like that? Or is there something wrong with my code somewhere?

EDIT

After post, i run firebug and it seems to show this error when I click on any of the textboxes in the dialog (which I don't know what would it mean)

f is undefined
[Break On This Error] "");f.settings[g]&&f.settings[g].call(.../\s/),function(g,h){b[h]=e})});var d=

jquery....min.js (line 22)

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文