asp.net-mvc 提交MVC后在同一视图中显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29624853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:26:08  来源:igfitidea点击:

Show result in same view after submit MVC

asp.net-mvcasp.net-mvc-4razorview

提问by Sachu

I have a form with two fields which will pass values to a stored procedure. The stored procedure will return 0 or 1.

我有一个包含两个字段的表单,它将值传递给存储过程。存储过程将返回 0 或 1。

If 0 the user is not eligible to see the data. If 1, he is. Then I want to show the details in the same page which I have submitted. I am using MVC 4 razor.

如果为 0,则用户没有资格查看数据。如果是 1,他就是。然后我想在我提交的同一页面中显示详细信息。我正在使用 MVC 4 剃刀。

Please give some idea how to achieve this. I am new to MVC.

请给出一些想法如何实现这一点。我是 MVC 的新手。

回答by Firoz Jafri

You can use javascript ajax to post your from. using this you can get the data through ajax without refreshing the page and then you can use your ajax result to display the data.

您可以使用 javascript ajax 发布您的信息。使用这个你可以通过ajax获取数据而无需刷新页面,然后你可以使用你的ajax结果来显示数据。

HTML code

HTML代码

<form id="MyForm"  method="post">
<input type="text" name="value1"/>
<input type="text" name="value2"/>
<input type="submit" name="Submit" value="Submit" />
</form>

Controller code

控制器代码

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult MyAction(MyViewModel model)
    {
        var result = ResultOfStoredPrcedure();
        return Content(result);
    }
}

javascript code

javascript代码

<script >

 $(document).ready(function () {

    $('#MyForm').submit(function (e) {

        var formData = new FormData(this);

        $.ajax({
            url: '@Url.Action("MyAction", "Home")',
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: formData,
            contentType: false,
            processData: false,
            success: function (result) {
             // here in result you will get your data
            },
            error: function (result) {

            }
        });
        e.preventDefault();
    });
});
</script>

回答by JonE

If you want you can utilise MVCs Ajax Helper, and use the Ajax.BeginForm(), or use javascript and a standard form to post. Whatever the choice, in your Action just return a View.

如果你愿意,你可以使用MVCs Ajax Helper,并使用Ajax.BeginForm(),或者使用javascript 和标准表单来发布。无论选择什么,在你的 Action 中只返回一个 View。

If you use the Ajax.BeginForm() you can specify an element by it's ID to update, and by returning a View you have greater control over what is returned in comparison to returning Content.

如果您使用 Ajax.BeginForm(),您可以通过要更新的 ID 指定一个元素,并且通过返回一个视图,与返回内容相比,您可以更好地控制返回的内容。

@using (Ajax.BeginForm("MyActionHome", "Home", new AjaxOptions {HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "resultArea"}))
{
    <input type="submit" name="Submit" value="Submit" /><
}

<div id="resultArea">
</div>   

This form allows you to specify the Action,Controller, Options and if you want additional arguments to send. We have also specified the TargetId that we want to update, in this case the 'resultArea'.

此表单允许您指定操作、控制器、选项以及是否要发送其他参数。我们还指定了要更新的 TargetId,在本例中为“resultArea”。

If you needed some clientside code to execute you could also use the OnComplete option and provide a JavaScript function.

如果您需要执行一些客户端代码,您还可以使用 OnComplete 选项并提供 JavaScript 函数。

[HttpPost]
public ActionResult PurchaseUnit()
{
    return View("_ResultPartial",);
}

Here we have a basic controller which returns a PartialView. That Partial will then be inserted into the Id specified, and will Replace, as defined by our options

这里我们有一个返回 PartialView 的基本控制器。然后该 Partial 将被插入到指定的 Id 中,并将替换,如我们的选项所定义的

回答by Baximilian

If I understand you correctly you need to use Ajax.Beginform (or Ajax.Action). So, have a look on this answer I hope it will help you https://stackoverflow.com/a/5410121/2115584

如果我理解正确,您需要使用 Ajax.Beginform(或 Ajax.Action)。所以,看看这个答案我希望它会帮助你 https://stackoverflow.com/a/5410121/2115584