【发布时间】:2019-07-17 16:18:44
【问题描述】:
当用户未完成表单时,我将传递一个带有“错误”值的 ViewBag,并且使用 jquery 我将 ViewBag 置于警报中,以便用户可以看到该消息。但我没有得到任何结果。
另外,我知道 .net 中的 GET 和 POST 是 2 个不同的页面,GET 显示视图,POST 返回显示视图但包含经过验证的数据。在我看来,我看到实际上只显示了一个页面,因为我已经放了一个 alert() 来确定它是否为真并且当页面加载时出现消息,但是当我按下提交按钮时,消息没有出现再次。
查看
@model wsCharlas.Models.ClsPrueba
@{
ViewBag.Title = "Prueba";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Prueba</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ClsPrueba</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.id_prueba, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.id_prueba, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.id_prueba, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.nombre_prueba, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.nombre_prueba, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.nombre_prueba, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script type="text/javascript">
$(document).ready(function () {
alert("Hello World");
if ('@ViewBag.msg' != "") {
alert('@ViewBag.msg');
}
});
</script>
控制器
public ActionResult Prueba()
{
return View(new ClsPrueba());
}
[HttpPost]
public ActionResult Prueba(ClsPrueba prueba)
{
if (!ModelState.IsValid)
{
ViewBag.msg = "error";
return View(prueba);
}
return View(prueba);
}
【问题讨论】:
-
jajajajaja...致敬兄弟...
标签: javascript jquery asp.net-mvc visual-studio