在MVC中编辑/添加记录使用Jquery Dialog不会在Dialog中显示来自服务器的错误

时间:2014-04-16 03:58:41

标签: jquery jquery-ui asp.net-mvc-4 modal-dialog jquery-dialog

这里我正在使用jquery对话框窗口进行添加,编辑,具有相同的局部视图,获取和发布两个操作的方法。 这就是我在点击时打开对话框的方式(下面显示的对话框用于创建新记录)

$("#openDialog").live("click", function (e) {
                e.preventDefault();
                var url = $(this).attr('href');

                $("#dialog-edit").dialog({
                    title: 'Add Student',
                    autoOpen: false,
                    resizable: false,
                    height: 355,
                    width: 400,
                    show: { effect: 'drop', direction: "up" },
                    modal: true,
                    draggable: true,
                    open: function (event, ui) {
                        $(this).load(url);
                    },
                    close: function (event, ui) {
                        $(this).dialog('close');
                    }
                });

                $("#dialog-edit").dialog('open');
                return false;
            });

这是我将内容加载到Jquery Dialog的部分视图

@using (Html.BeginForm("AddEditRecord", "Home"))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Student</legend>
    <div class="editor-field">
            @Html.HiddenFor(model => model.StudentID)
        @Html.ValidatioMessageFor(model =>model.StudentID)
    </div>
        <div class="editor-label">
            @Html.EditFor(model => model.Name)   
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Age)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.State)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.State)
            @Html.ValidationMessageFor(model => model.State)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Country)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Country)
            @Html.ValidationMessageFor(model => model.Country)
        </div>
        <p>
            @if (ViewBag.IsUpdate == true)
            {
                <input type="submit" value="Update" id="btnUpdate" name="cmd"/>
            }
            else
            {
                <input type="submit" value="Save" id="btnSave" name="cmd"/>
            }
            @Html.ActionLink("Back to List", "Index")
        </p>
    </fieldset>
}

以下是获取要在对话框中显示的记录的方法

[HttpGet]
        public ActionResult AddEditRecord(int id?)
        {
        if(id!=null)
        {
                    Student student = db.Students.Where(m => m.StudentID == id).FirstOrDefault();
                    return PartialView("_StudentData", student);
                }
                return PartialView("_StudentData");
            }            
        }

下面是我在输入详细信息到对话框表单后提交记录的Post方法 对话框数据将发布到此方法

[HttpPost]
public ActionResult AddEditRecord(Student student, string cmd)
{
   if (ModelState.IsValid)
   {
      if (cmd == "Save")
      {
         try
         {
            Student student = db.Students.Where(m => m.StudentID == id).FirstOrDefault();
            if(student!=null)
            {
               db.Students.Add(student);
               db.SaveChanges();
               return RedirectToAction("Index");
            }
            else
            {
               ModelState.Error("", "Record already exists");
               return PartialView("_StudentData", student);
            }   
         }
         catch { }
      }
         else
         {
            try
             {
               Student stud = db.Students.Where(m => m.StudentID == student.StudentID).FirstOrDefault();
               if (stud != null)
               {
                  stud.Name = student.Name;
                  stud.Age = student.Age;
                  stud.State = student.State;
                  stud.Country = student.Country;
                  db.SaveChanges();
                  return RedirectToAction("Index");
               }
             }
             catch { }
         }
      }
     return PartialView("_StudentData", student);
}

On Submitting Modal验证工作正在显示对话框本身的错误, 我试图在用户提交记录时在帖子中执行相同的操作,如果没有,则检查表中是否有任何重复的StudentID,然后将记录保存到数据库并重定向到索引(学生页面列表)。 但是如果存在记录,那么我想在对话框中向用户显示存在具有相同学生ID的记录但是UI重定向并且对话框显示在父窗口而不是对话框中。 我做了一些研究,但没有找到答案来实现这一目标。

0 个答案:

没有答案
相关问题