2

在这个剃刀视图中,我有两个按钮,分别称为上一个和下一个。当用户按下这些按钮时,我需要调用一个存储用户类型的方法:

@using (Html.BeginForm("Index", "Quiz", FormMethod.Post))
{
<div style="border-bottom: 2px solid #c8c8c8; overflow: auto; width: 100%">
    <h2 class="float-left" style="padding-bottom:5px;">@Model.Quiz.Name</h2>

    <div class="float-right">
        <input name="button" type="submit" value="done" />
    </div>
</div>

@for( ...) {
...
}

<div class="fixednavcontainer">
    <div id="questionnav" class="content-wrapper">
        <div id="questionnavstatus" class="float-left">
            <p>
                Question <span id="currentPage">@ViewBag.CurrentPage</span> of <span id="totalPages">@ViewBag.TotalPages</span>
            </p>
        </div>

        <div id="navbuttons" class="float-right">
            <img id="previous" src="~/Images/previous.png" />
            <img id="next" src="~/Images/next.png" />
        </div>

        <div class="clear-fix" />
    </div>
</div>

像这样的东西应该调用按钮

private void Save(@model model)
{
    ...
}

我希望 ajax 不会刷新页面的任何内容或加载另一个页面,只需保存并保持同一页面。自 jquery 以来是否有可能调用一个动作方法(我猜)?

4

2 回答 2

2

您还可以使用 jQuery 并像这样进行调用:

var myModel = @Html.Raw(Json.Encode(MyModel));  //MVC3

$.ajax({
    type: "POST",
    url: "/MyController/SomeAction/",
    data: myModel ,
    cache: false,
    dataType: "json",
    success: function (response) {
        // Do whatever you have to do...
    }
});

在你的控制器中是这样的:

public class MyController
{
    [HttpPost]
    public ActionResult SomeAction(MyModel myModel)
    {
        // Do whatever and return whatever
            return true;
    }
}
于 2013-02-05T06:03:02.070 回答
2

你可以

$.ajax({
    type: "POST",
    url: "@Url.Action('yourAddress')",
    data: parametr,
    cache: false,
    dataType: "json",
    success: function (response) {
        // Do whatever you have to do...
    }
});

并在控制器中

public class XController
{
    [HttpPost]
    public ActionResult yourAddress(YourModel)
    {
...
    }
}
于 2013-02-05T06:11:17.730 回答