如何使用jquery或ajax为MVC项目更新c#/ asp.net中的razor部分视图

在MVC部分视图文件中,我构建了一个Html.TextBox和两个提交按钮。单击这两个按钮将增加/减少Html.TextBox值。Html.TextBox显示的值将相应地更改。但是,一旦我需要单击后根据新值更新#refTable div。页面或部分从未更新。下面是代码,其中添加了一些注释以用于说明。谢谢你的帮助。


// * ** * *** cshtml文件 ** * ** * **** //


<body>

</body>


<input type="submit" value="PrevY" name="chgYr2" id="pY" />

@{

    var tempItem3 = Model.First(); // just give the first entry from a database, works.

    if (ViewData["curSel"] == null)

    {

    @Html.TextBox("yearSelect3", Convert.ToDateTime(tempItem3.Holiday_date).Year.ToString());  

    ViewBag.selYear = Convert.ToDateTime(tempItem3.Holiday_date).Year; // just initial value, works

    ViewData["curSel"] = Convert.ToDateTime(tempItem3.Holiday_date).Year;

    }

    else

    {

    @Html.TextBox("yearSelect3", ViewData["curSel"].ToString());

    } 

}

<input type="submit" value="NextY" name="chgYr2" id="nY" />



<script type="text/javascript">

    $(document).ready(function () {

        $(document).on("click", "#nY", function () {

            var val = $('#yearSelect3').val();

            $('#yearSelect3').val((val * 1) + 1);

            var dataToSend = {

                id: ((val * 1) + 1)

            }

            // add some jquery or ajax codes to update the #refTable div

            // also ViewBag.selYear need to be updated as ((val * 1) + 1)

            // like:   ViewBag.selYear = ((val * 1) + 1);

            // any similar temp variable is fine

        });


        });

        $(document).on("click", "#pY", function () {

            var val = $('#yearSelect3').val();

            $('#yearSelect3').val((val * 1) - 1);

            var dataToSend = {

                id: ((val * 1) - 1)

            }


        });

    });

</script>




<span style="float: right"><a href="/">Set Holiday Calender for 2013</a></span>

<span id="btnAddHoliday">@Html.ActionLink("Add Holiday", "Create", null, new { id = "addHilBtn" })</span>

    </table>

</div>


偶然的你
浏览 818回答 3
3回答

慕沐林林

您也可以像这样使用Url.Action作为路径:$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: "@Url.Action("Holiday", "Calendar", new { area = "", year= (val * 1) + 1 })",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; type: "GET",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; success: function (partialViewResult) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#refTable").html(partialViewResult);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP