[英]Get button clicked ID in “OnSuccess” function after form submit
我在mvc3中使用Ajax表單。
下面是代碼。
<% using (Ajax.BeginForm("Method", "Conroller", new AjaxOptions
{
UpdateTargetId = "PopupBody",
HttpMethod = "post",
OnSuccess = "OnSuccessContactInfoSave"
}, new { @id = "frmContactInfo" }))
{ %>
function OnSuccessContactInfoSave( data, textStatus ) {
alert( 'completed with success.' );
}
現在,我在頁面上有2個按鈕,一個是提交按鈕,另一個是普通按鈕。 現在,我想知道Onsuccess函數中單擊的按鈕。
如何在“ OnSuccessContactInfoSave”功能中獲取它?
提前致謝
編輯:
這是我的看法
<% using (Ajax.BeginForm("SaveContactInfo", "ManageUser", new AjaxOptions
{
UpdateTargetId = "PopupBody",
HttpMethod = "Post"
}))
{ %> <div class="ciMain">
<input type="submit" id="btnSaveAndClose" name="btn" value="Save" />
<input type="submit" value="Save and continue to next step" name="btn" />
<input type="button" value="Cancel" />
</div>
<% } %>
這是管制員
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveContactInfo(FormCollection userViewModel, ContactInfoViewModel model, string btn)
{
//string test = Request["btn"].ToString();
try
{
return View("ContactInfo", model);
}
catch (Exception)
{
return View("ContactInfo", model);
}
}
首先,您需要在模型類ContactInfoViewModel中創建一個名稱為SubmissionType的屬性,如下所示:
public class ContactInfoViewModel
{
public string SubmissionType { get; set; }
//Your rest of properties
}
現在在您的視圖中,在“提交”按鈕中傳遞此屬性名稱,如下所示:
<input type="submit" name="SubmissionType" id="btnSumit" value="Submit"/>
<input type="submit" name="SubmissionType" id="btnOther" value="Other"/>
請記住,這些按鈕必須在form標簽下,也不要忘記將模型與視圖綁定,如下所示:
@model ClassNamespace.ContactInfoViewModel
現在您必須像這樣重組您的操作方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveContactInfo(ContactInfoViewModel model)
{
if (model.SubmissionType == "Submit")
{
}
else
{
}
try
{
return View("ContactInfo", model);
}
catch (Exception)
{
return View("ContactInfo", model);
}
}
現在進入ajax表單標簽,您還必須在此處傳遞Model ,以便在提交表單時獲取模型的值。 像這樣做:
@using (Ajax.BeginForm("SaveContactInfo", "ManageUser",Model, new AjaxOptions
{
UpdateTargetId = "PopupBody",
HttpMethod = "Post"
}))
如您在上面的代碼中看到的,我也將模型作為對象routeValues傳遞了。
希望現在這能解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.