简体   繁体   English

表单提交状态成功/失败

[英]form submission status success/fail

I have a very simple form like so... 我有一个非常简单的表格,就像这样...

<form id="myform" enctype="multipart/form-data" action="/api/test/Post" method="post">
    <input name="file2" type="file" />
    <input type="text" name="text1" />

    <input type="submit" name="submit" value="click click" />
</form>

This in turns call a RESTful service which looks like.... 依次调用一个看起来像...的RESTful服务。

public void Post()
    {
        var httpRequest = HttpContext.Current.Request;
        string value1 = httpRequest .Form["text1"];
        //do stuff with the form data submitted

        var files = new List<string>();

        foreach(string file in httpRequest .Files)
        {
            var postfeild = httpRequest .Files[file];
            //do stuff
        }          
    }

When the submit button is pressed on the form, the page refreshes. 在表单上按下“提交”按钮时,页面将刷新。 I would like to tell the user if their form has been submitted successfully, if there was an error, or redirect them to another page. 我想告诉用户他们的表单是否已成功提交,是否有错误,或将其重定向到另一个页面。

Is this possible to achieve at all? 这有可能实现吗? I thought about making an ajax call and just sending over stringified data, however, having files complicates things. 我曾考虑过进行ajax调用并只发送字符串化数据,但是,拥有文件会使事情变得复杂。

Is there anything I can do through C# or JavaScript to achieve the desired result? 我可以通过C#或JavaScript做任何事情以达到期望的结果吗?

I prefer to POST via AJAX and respond with a JSON object that contains status/message. 我更喜欢通过AJAX进行发布,并使用包含状态/消息的JSON对象进行响应。 However if you want to keep it classic then you'd have to inject some javascript that will run upon load. 但是,如果要使其保持经典,则必须注入一些可在加载时运行的javascript。

As for using javascript with web forms: (it's been a while since I've used it..) 至于将javascript与网络表单一起使用:(自从我使用它已经有一段时间了。)

pcode at best: pcode充其量:

<% if ( someVariable == true ){  Response.Write("alert('successful update of blah');"); } %>

You can add some sort of alert after the page reloads due to a form submission. 由于表单提交,页面重新加载后,您可以添加某种警报。 You just need to set some sort of flag before you submit the form and set it back after the message is displayed. 您只需要在提交表单之前设置某种标志,然后在显示消息后将其重新设置即可。 It will need to be a server side flag, a JS variable will be erased. 它必须是服务器端标志,JS变量将被删除。

You could also switch to AJAX with jQuery and you can submit a form without refreshing the page. 您也可以使用jQuery切换到AJAX,并且可以提交表单而不刷新页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM