我有一个索引页面,其中列出了“工作簿”标题,每个工作簿都有一个“共享”按钮。当按下按钮时,会出现一个引导程序模型(即对话框),该模型显示工作簿的标题和文本区域,允许用户输入共享电子邮件地址。
当用户按下“共享”按钮时,我正在调用一个javascript函数,该函数调用一个控制器操作,该操作返回一个包含模式对话框的局部视图,该模式对话框中包含表单。问题是,在按下提交按钮(即“共享”)之后,没有验证错误显示给用户,我不确定为什么会这样。有人可以提供一些想法吗?
这是我的主要( index.cshtml )页面:
@model DNAAnalysisCore.Models.WorkBookModel
@{
}
@section BodyFill
{
<script type="text/javascript">
function showSharingView(title) {
var url = "@Url.Action("ShowShareDialog", "Home")" + "?workbookTitle=" + encodeURI(title);
$('#shareFormContainer').load(url,
function() {
$('#shareFormModal').modal("show");
});
}
function hideSharingView() {
$('#shareFormModal').modal("hide");
}
</script>
<div id="shareFormContainer" >
<!--PLACEHOLDER FOR SHARE DIALOG -->
</div>
<div class="workbook-container">
<table class="table">
<tbody>
@foreach (var workbook in Model.Workbooks)
{
<tr>
<td>@Html.ActionLink(workbook.Name, "Open", "OpenAnalytics", new {id = Model.Id, workbook = workbook.Name})</td>
<td>
<button title="Share" class="share-button" onclick='showSharingView("@workbook.Name")'> </button>
</td>
</tr>
}
</tbody>
</table>
</div>
}
那是我的控制器:
public class HomeController : Controller
{
[HttpGet]
public IActionResult ShowShareDialog(string workbookTitle)
{
var shareModel = new ShareModel
{
Title = workbookTitle
};
return PartialView("_ShareView", shareModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult ShareWorkbook(string emails, string title)
{
var share = new ShareModel
{
Emails = emails
};
// TODO EMAIL THE SHARED WORKBOOK using the 'title' of the workbook and the 'email' string value
// return no content to avoid page refresh
return NoContent();
}
}
这是我的局部视图/模式对话框(_ShareView):
@using DNAAnalysisCore.Resources
@model DNAAnalysisCore.Models.ShareModel
<!-- Modal -->
<div class="modal fade" id="shareFormModal" role="dialog">
<div class="modal-dialog modal-md">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Share Workbook - @Model.Title</h4>
</div>
@using (Html.BeginForm("ShareWorkbook", "Home", FormMethod.Post))
{
<div class="modal-body">
<label>@BaseLanguage.Share_workbook_Instruction_text</label>
<div class="form-group">
<textarea class="form-control" asp-for="Emails" rows="4" cols="50" placeholder="@BaseLanguage.ShareDialogPlaceholder"></textarea>
@* TODO add client-side validation using jquery.validate.unobtrusive.js. See US268276 *@
<span asp-validation-for="Emails" class="text-danger"></span>
</div>
<input asp-for="Title" />
</div>
<div class="modal-footer">
<button type="submit" onclick="hideSharingView()" class="btn btn-primary">Share</button>
<button id="btnCancelDialog" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
}
</div>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
这是我的 ShareModel :
public class ShareModel
{
[HiddenInput]
public string Title { get; set; }
[Required]
public string Emails { get; set; }
}
答案 0 :(得分:1)
页面加载时不会将表单添加到页面上,不打扰的验证也不会进行。简单的解决方案是使用#define concat(x, y) concat_i(x, y)
#define concat_i(x, y) x##y
#define concat_if(cond, x, y) concat(concat_if_, cond)(x, y)
#define concat_if_0(x, y) (x)
#define concat_if_1(x, y) concat(x, y)
int concat_if(1, hello, 0); //int hello0;
int concat_if(0, hello, 1); //int (hello);
。请参考here。
1。在$.validator.unobtrusive.parse("#id-of-the-form");
部分视图中将id
添加到表单:
_ShareView
2。将验证文件@using (Html.BeginForm("ShareWorkbook", "Home", FormMethod.Post,new { @id="partialform"}))
引入主页(Index.cshtml),并使用不干扰用户的验证手动注册表单。
_ValidationScriptsPartial.cshtml