使用jQuery更新ViewBag并渲染结果

时间:2012-12-13 08:45:36

标签: jquery asp.net-mvc viewbag

我有一个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>

1 个答案:

答案 0 :(得分:0)

试试这个

在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脚本具有值调用操作。

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