ajax formdata action,asp.net mvc 3 - Post form data to Controller's action with Ajax - Stack Overflo...

在ASP.NET MVC3中,用户点击Ajax.ActionLink后,控制器的ACTION通过GET方法更新弹出对话框的内容。然而,表单提交使用POST方法导致页面刷新。尝试用Ajax.ActionLink替换提交按钮,但在ACTION链接渲染时,模型属性未设置,导致POST请求丢失数据。寻求解决Ajax提交表单并避免页面刷新的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a page in MVC3, with a link (Ajax.ActionLink). When user clicks it, it calls controller's action, and the result is inserted into a div, with replace.

Code is shown below:

@Ajax.ImageActionLink("/Images/btn_share.png", "Share pool", "SharePool", new { poolKey = Model.Id, poolName = Model.Name },

new AjaxOptions {

UpdateTargetId="popup",

HttpMethod="GET",

InsertionMode = InsertionMode.Replace,

LoadingElementId="loading_dialog",

OnSuccess = "ShowPopup('#popup_share', true, true)"

}

ImageLinkAction is custom extension method to use image as link, and ShowPopup is a javascript function that shows the updated div (to make it look as a popup)

Now the markup code inserted into the div which creates the popup contains a form as below

@using (Html.BeginForm()) {

@Html.HiddenFor(model => model.ID)

@Html.LabelFor(model => model.EmailAddress)

@Html.EditorFor(model => model.EmailAddress)

@Html.ValidationMessageFor(model => model.EmailAddress)

// ... other fields

@Html.ValidationSummary(true)

Share

}

The issue is with the form's submit: the Submit button calls the proper action but with a postback, which cause my page to refresh. What I need is to post data with ajax, receive the response, which is another partial view that gets inserted into the

I was trying to replace the Submit button with Ajax.ActionLink as below

@Ajax.ActionLink("Share", "Share",

new Models.MyModel

{

ID = Model.ID,

EmailAddress = Model.EmailAddress

},

new AjaxOptions

{

UpdateTargetId="popup",

HttpMethod="POST",

InsertionMode = InsertionMode.Replace,

LoadingElementId="loading_dialog",

OnSuccess = "ShowPopup('#popup_share', true, true)"

}

The controller's code looks like this:

[HttpPost]

public ActionResult SharePool(MyModel model)

{

// ...

return PartialView("_MyPartialView", model)

}

The problem is, in the moment the Ajax ActionLink is rendered (when form is loaded) there is no value in Model.EmailAddress, so my POST action in controller receives only ID parameter.

How can I handle this? Ideally, I think I should add

OnBegin = "PreparePostData()"

But since I know javascript only basically, I have no idea how can I implement this. I think this PreparePostData() should collect form fields and prepare the object routeValues parameter, to be set before the ajax call is invoked.

Anyone can give me some indications on how to implement this?

Or is any other, better approach on this problem?

Thank you

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值