提交MVC后在同一视图中显示结果 [英] Show result in same view after submit MVC

查看:61
本文介绍了提交MVC后在同一视图中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个字段的表单,这些表单会将值传递给存储过程.存储过程将返回0或1.

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,则为.然后,我想在提交的同一页面上显示详细信息.我正在使用MVC 4剃须刀.

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.

请提供一些有关如何实现此目标的想法.我是MVC的新手.

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

推荐答案

您可以使用javascript ajax发布您的发件人.使用此功能,您无需刷新页面即可通过ajax获取数据,然后可以使用ajax结果显示数据.

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.

HTML代码

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

控制器代码

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

javascript代码

javascript code

<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>

这篇关于提交MVC后在同一视图中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆