【问题标题】:How to get the value of a ViewBag with Jquery如何使用 Jquery 获取 ViewBag 的值
【发布时间】: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


【解决方案1】:

有一个解决办法。它是一个快速的取自 Possible to access MVC ViewBag object from Javascript file?

在 HTML 中

<input type="hidden" id="customInput" data-value = "@ViewBag.CustomValue" />

在 JS 中

var customVal = $("#customInput").data("value");

你可以去那里为答案投票。

【讨论】:

  • 使用数据属性来做这件事有点矫枉过正......只需使用value="@ViewBag.msg",然后在脚本中调用$("customInput").val()。差别不大,但保持简单:)
猜你喜欢
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
相关资源
最近更新 更多