2015-07-13 66 views
0

的局部視圖中查找單擊哪個按鈕我在局部視圖中有2個按鈕(保存和預覽)。這部分視圖正在執行Ajax帖子,並且兩個按鈕的工作都很好,其中Preview僅允許預覽小部件,而Save則將數據保存到數據庫中。這裏我有2個問題如何從onSuccess Ajax調用

  1. 我無法找到哪個按鈕被點擊(從Ajax.BeginForm onSuccess)。
  2. 我的控制器不重定向到操作鏈接

公衆的ActionResult EditWidget(WidgetViewModel視圖模型,字符串的onSave,串onPreview)

{

if(!string.IsNullOrEmpty(onPreview)) 
    { 
     viewmodel.ButtonName = "Preview"; 

     var parameters = ""; 

     if (!string.IsNullOrEmpty(viewmodel.OverriedParameters)) 
      parameters = viewmodel.OverriedParameters; 
     else 
      parameters = viewmodel.DefaultParameters; 

     return PartialView("Widgets/_Preview", previewViewModel); 
    } 
    else if(!string.IsNullOrEmpty(onSave)) 
    { 
     viewmodel.ButtonName = "Save"; 

     if(ModelState.IsValid) 
     { 
      //RedirectToAction("GetPage", "Home", new { Page = pageId 
      return Json(Url.Action("GetPage", new {Page = pageId})); 
     } 
    } 

    return null; 
} 

我的部分觀點如下

@using (Ajax.BeginForm("EditWidget", "Home", new AjaxOptions { UpdateTargetId = "divPreview", HttpMethod = "POST", OnBegin = "application.getpage.onBeginPreview", OnSuccess = "application.getpage.onSuccessPreview" })) 
    { 
     <div class="form-inline"> 
      @Html.LabelFor(x => x.OverriedParameters, "Is Widget Active ?", new { @class = "col-sm-2" }) 
      @Html.TextBoxFor(x => x.OverriedParameters, new { @class = "col-sm-1" }) 
     </div> 

     <div class="form-inline"> 
      @Html.LabelFor(x => x.DefaultParameters, "Is Widget Active ?", new { @class = "col-sm-2" }) 
      @Html.TextBoxFor(x => x.DefaultParameters, new { @class = "col-sm-1" }) 
     </div> 

    <div class="form-inline"> 
     <input id="btnSave" name="onSave" type="submit" class="btn btn-primary" style="width:65px" value="Save" /> 
     <input id="btnPreview" name="onPreview" type="submit" class="btn btn-primary" style="width:65px" value="Preview" /> 
    </div> 

    } 

<div id="divPreview" style="margin-top:5px"> 
</div> 

and my onSuccess is a S請按照

onSuccessPreview: function (data, textStatus, jqXHR) { 
// Here i want to find which button is clicked from my partial view. 
// On Save button click i want to redirect to the action method in the same controller while on Preview button i don't want to do anything 
    } 
+0

我想預覽按鈕不應該發佈表單。將其類型更改爲'button'而不是'submit'。 – Mairaj

+0

帶有類型按鈕,我無法調用負責呈現預覽部分視圖的操作方法。 –

回答

0

你可以添加一個隱藏輸入:在你的onSucces使用的onclick Javascript方法

<div class="form-inline"> 
    <input id="btnSave" name="onSave" type="submit" class="btn btn-primary" style="width:65px" value="Save" /> 
    <input id="btnPreview" name="onPreview" type="submit" class="btn btn-primary" style="width:65px" value="Preview" /> 
    <input type="hidden" id="what-was-clicked" /> 
     </div> 

更新它的值(前提交),並閱讀:

onSuccessPreview: function (data, textStatus, jqXHR) { 
    var clickedButton = $("#what-was-clicked").val(); 
    } 
+0

非常感謝你vhr。這正是我期待的。 –

+0

歡迎您:) – vhr