【问题标题】:Update ViewBag with jQuery and rendering results使用 jQuery 更新 ViewBag 并渲染结果
【发布时间】:2012-12-13 08:45:35
【问题描述】:

我有一个 MVC3 应用程序,它使用 ViewBag 变量从数据库中动态获取数据并动态生成界面。

我正在使用 jQuery 调用 action 方法将一些值插入数据库并相应地更新 ViewBag 变量。我想用从服务器更新后返回的新 ViewBag 值刷新 div ......真的不知道该怎么做。谢谢

这是我的查看代码:

    <div id="phys">
        <fieldset>
            <legend>IPSS</legend>


                @foreach (var parameter in ViewBag.four.Parameters)
                {

                    @MyHelpers.ParameterList(parameter, ViewBag.four.ParametersValues, "ParametersList")


                }
                <button type="button" onclick="print()" class="t-button">
        Add Physician</button>

        <table>
    <thead>
        <th>
            Physician
        </th>
        <th>
            Time
        </th>
        <th>
            Delete
        </th>


    </thead>

    <tbody>
        @foreach (var row in ViewBag.physicians.ParametersValues)
        {
            <tr>
                @{var items = ((string)row.Value).Split('|');}

                <td>@items[0]
                </td>
                <td>@items[1]
                </td>
                <td>@Html.ActionLink("Delete", "DeleteVisitParameter", new { vpid = @row.VisitParameterID })</td>

            </tr>
        }
    </tbody>

</table>

        </fieldset>
        </div>

这是我的 jQuery:

<script type="text/javascript">
    $(document).ready(function () {

        print = function () {

            //$.post("/visit/physicians", { phy: $("#ParamList7 option:selected").text(), vID: '@ViewBag.VisitID' })

            $.ajax({
                cache: false,
                type: "POST",
                url: '@Url.Content("~/visit/physicians")',


                data: { phy: $("#ParamList7 option:selected").text(), vID: '@ViewBag.VisitID' },
                success: function (data) {

                    //What Shall I do here to update div #phys with new View Data??
                },
                fail: function (msg) {
                    alert("Fail"); ;
                }
            });



        };


    });

</script>

【问题讨论】:

    标签: jquery asp.net-mvc viewbag


    【解决方案1】:

    试试这个

    修改了javascript中的AJAX方法

    $.ajax({
                            cache:false,
                            type: "GET",
                            url: "@(Url.Action("visit", "physicians"))",
                            data: { phy: $("#ParamList7 option:selected").text(), vID: '@ViewBag.VisitID'},
                            success: function (data) {
    
                               // perform operation after successful call 
    
    
                            },
                            error:function (xhr, ajaxOptions, thrownError){
                                alert('fail');
    
                            }  
                        });
    

    //在控制器中

    public ActionResult visit(string phy,string vID)
    {
    
      //perform your operation here and return result in JSON 
    
       return Json(new {result =""},JsonRequestBehavior.AllowGet);
    }
    

    注意:确保所有参数都必须具有通过 java 脚本调用的值。

    这将调用你的动作,如果想在执行功能后更新值,那么你必须将结果传递给 JSON 并在 java 脚本的 成功函数 中执行操作

    【讨论】:

    • 非常感谢您的回复,我使用了您的代码并返回了新的 ViewBag 值如下
    • 好的。非常感谢您的回复,我确实使用了您的代码并在结果中返回了新的更新 ViewBag,如​​下所示: ViewBag.physicians = GetVisitSectionParamters(vID, 100).ParametersValues[0].Value; return Json(new { result = ViewBag.physicians }, JsonRequestBehavior.AllowGet
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2021-11-02
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    相关资源
    最近更新 更多