使用Jquery对话框在MVC中编辑/添加记录不会在对话框中显示来自服务器的错误
在这里,我使用相同的局部视图、get和post方法为这两个操作添加、编辑jquery对话框窗口。 这就是我在单击时打开对话框的方式(下面显示的对话框用于创建新记录) 这是我将内容加载到Jquery对话框的部分视图使用Jquery对话框在MVC中编辑/添加记录不会在对话框中显示来自服务器的错误,jquery,jquery-ui,asp.net-mvc-4,modal-dialog,jquery-dialog,Jquery,Jquery Ui,Asp.net Mvc 4,Modal Dialog,Jquery Dialog,在这里,我使用相同的局部视图、get和post方法为这两个操作添加、编辑jquery对话框窗口。 这就是我在单击时打开对话框的方式(下面显示的对话框用于创建新记录) 这是我将内容加载到Jquery对话框的部分视图 @using (Html.BeginForm("AddEditRecord", "Home")) { @Html.ValidationSummary(true) <fieldset> <legend>Student</le
@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>
}
下面是我在对话框表单中输入详细信息后提交记录的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);
}
提交模式验证时,对话框本身显示错误,
当用户提交记录时,我尝试在帖子中做同样的操作。这将检查表中是否有重复的StudentID。如果没有,记录将保存到数据库并重定向到索引(学生列表页面)。
但是如果有记录存在,那么我想在对话框中向用户显示一个具有相同学生ID的记录存在,但是UI重定向,并且对话框显示在父窗口而不是对话框中。
我做了一些研究,但没有找到解决办法。使用ajax表单代替html表单也尝试过,但仍然重定向到主窗口。
[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");
}
}
[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);
}