在MVC 4中的同一弹出对话框中显示模型错误 [英] Display model error on same popup dialog in MVC 4
问题描述
我正在开发具有Index.cshtml的应用程序(带搜索条件和搜索按钮的主要页面),_PartialView.cshtml(显示带有更新/删除链接的记录的部分视图),Edit.cshtml(用于更新的视图)。
点击更新链接后,打开弹出模式对话框,其中包含我应用的编辑视图(Edit.cshtml)必需& StringLength属性到某些字段。如果某些属性未满足,我想在同一个弹出窗口中显示它们。但是在控制器动作方法中,我正在返回View,因此模态弹出消失了&编辑视图在div中打开。
我的记录在partialView(_PartialView.cshtml)中显示为网格,更新链接和javascript函数为
I am developing application which has Index.cshtml (main pg with searching criteria & Search button), _PartialView.cshtml (partial view displaying records with update/delete links), Edit.cshtml (View for updating).
After clicking Update link, popup modal dialog is opened containing Edit view (Edit.cshtml) I applied Required & StringLength attribute to some fields. If some of attributes not fulfilled, i want to display them in same popup. But in controller action method, I am returning View so modal popup disappears & Edit view is opened in div.
My records are displayed as grid in partialView (_PartialView.cshtml) with Update link and javascript function as
<a href="javascript:void(0);" class="anchorDetail" onclick="editFunc(@item.SrNo)" ">Update</a>
function editFunc(id) {
var TeamDetailPostBackURL = '/Home/Edit';
var $buttonClicked = $(this);
var options = { "backdrop": "static", keyboard: true };
$.ajax({
type: "GET",
url: TeamDetailPostBackURL,
cache: false,
contentType: "application/json; charset=utf-8",
data: { "SrNo": id },
datatype: "json",
success: function (data) {
$('#myModalContent').html(data);
$('#myModal').css('display', 'block');
$("#myModal").dialog({
title: 'Edit Data',
modal: true,
bgiframe: true,
show: 'slide',
hide: 'slide',
width: 750
});
$('#myModal').dialog("show");
},
error: function () {
alert("Dynamic content load failed.");
}
});
}
我的Edit.cshtml:
My Edit.cshtml :
@using (Ajax.BeginForm("Edit", "Home", new AjaxOptions { UpdateTargetId = "divPartial", HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnBegin = "CallEditBegin", OnSuccess = "CallUnblock" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label" style="display:none" >
@Html.LabelFor(model => model.SrNo)
</div>
<div class="editor-field" style="display:none">
@Html.EditorFor(model => model.SrNo)
@Html.ValidationMessageFor(model => model.SrNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReaderName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReaderName)
@Html.ValidationMessageFor(model => model.ReaderName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UploadDate)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.UploadDate, new { @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.UploadDate)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
我的HomeController.cs
My HomeController.cs
[HttpPost]
public ActionResult Edit(UploadData uploadData)
{
if (!ModelState.IsValid)
{
return View(uploadData);
}
else
{
// Processing Logic here
string strResult = uploadDataBusinessLayer.InsertUpdateUploadData(uploadData, "Update");
if (strResult == "Success")
{ // Do return Partial View
}
else
{ // How to display exception error in same popup ???
}
}
}
现在需要提交一些文件,而ReaderName的最小长度为6个字符。如果任何条件未满足
Now some filed are required and ReaderName has restiction of min length of 6 characters. If any of the condition not fulfilled
if (!ModelState.IsValid)
{
return View(uploadData);
}
此块将被执行。
但接下来发生了什么,我的弹出对话框消失了& div将在主pg上打开显示错误消息。
我想在控件前弹出窗口显示它们。
我很困惑如果出现模型错误该怎么办。
请给出一些建议,我缺少什么.....
我的尝试:
搜索谷歌但无法找到任何合适的信息.......
this block will be executed.
But what happened next, my popup dialog disappears & div will be open on main pg displaying error message.
I want to show them on popup itself in front of control.
I am confusing what to do in case of model errors.
Please give some advice, what I am missing.....
What I have tried:
Searched on google but cant find any suitable information.......
推荐答案
buttonClicked =
buttonClicked =
(此跨度>);
var options = { backdrop: static,键盘: true };
(this); var options = { "backdrop": "static", keyboard: true };
.ajax({
type: GET,
url:TeamDetailPostBackURL,
cache: false ,
contentType: application / json; charset = utf-8,
data:{ SrNo:id},
数据类型: json,
成功: function (数据){
.ajax({ type: "GET", url: TeamDetailPostBackURL, cache: false, contentType: "application/json; charset=utf-8", data: { "SrNo": id }, datatype: "json", success: function (data) {
这篇关于在MVC 4中的同一弹出对话框中显示模型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!