在提交按钮的onclick()事件和View中的表单提交之间保存数据

时间:2021-07-29 20:08:14

Form below is posted from view to action method. onclick() event invokes jquery.ajax and passes data(content of the first div) to the same action method. jquery.ajax is executed first and form submission comes next. How can I have the value of pid (passed by jquery.ajax) when the form submitted? I need to use sth like Session, don't I?

以下表格从视图发布到操作方法。 onclick()事件调用jquery.ajax并将数据(第一个div的内容)传递给相同的操作方法。首先执行jquery.ajax,接下来是表单提交。当表单提交时,如何获得pid的值(由jquery.ajax传递)?我需要像会话一样使用,不是吗?

View:

<form id="newform" action="/ad/newad" method="post">
    <div id="divAjax">
       <p align="center" style="background: white; margin: 0cm 0cm 0pt;">Dan</p>
       <p align="center" style="background: white; margin: 0cm 0cm 0pt;">
       <b><span contenteditable="true"> CLICK TO CHANGE</span></b>
       </p>
    </div>
    <div class=" content">
       <input type="text" name="PName"/>
       <input type="text" name="SName"/>
       <input type="submit" value="Submit" id="btnSubmit" onclick="return updatePerson();" />
    </div>
</form>
<script>
function updatePerson(){
   $.ajax({
       url         : "/ad/newad",
       type        : "POST",
       dataType    : "json",
       contentType : "application/json;",
       data        : "{ pid:'" + $("#divAjax").html() + "' }"       
   });
</script>

Action Method:

[HttpPost]
[ViewException]
[UserFilter(OpUserAuthType.Admin, OpUserAuthType.Normal)]
public ActionResult NewAd(string id, string pid, AdDetail model)
{
    ...
}

1 个解决方案

#1


0  

I think you just need to not have a submit button - just a regular button. Your form submits to the same action as your ajax post anyway. I think maybe you just need to post all of your data at once in the click handler, not just your pid and you might not need the submit I think.

我想你只需要一个提交按钮 - 只需一个常规按钮。无论如何,您的表单都会提交与您的ajax帖子相同的操作。我想也许您只需要在点击处理程序中一次发布所有数据,而不仅仅是您的pid,您可能不需要我认为的提交。

You can then use the success: or error: handler of the ajax call if you need to do something after the post has been made.

然后,如果您需要在发布后执行某些操作,则可以使用ajax调用的success:或error:handler。

#1


0  

I think you just need to not have a submit button - just a regular button. Your form submits to the same action as your ajax post anyway. I think maybe you just need to post all of your data at once in the click handler, not just your pid and you might not need the submit I think.

我想你只需要一个提交按钮 - 只需一个常规按钮。无论如何,您的表单都会提交与您的ajax帖子相同的操作。我想也许您只需要在点击处理程序中一次发布所有数据,而不仅仅是您的pid,您可能不需要我认为的提交。

You can then use the success: or error: handler of the ajax call if you need to do something after the post has been made.

然后,如果您需要在发布后执行某些操作,则可以使用ajax调用的success:或error:handler。