2011-01-19 15 views
0

私はボタンがあります。ASP.NET MVC:モデルバインディングとAjaxリクエスト

<a id="2" class="modalInput specialbutton" href="/Employee/Delete/2" rel="#yesno"><img src="/Content/Images/application_delete.png" alt="Delete" /></a> 

Javascriptのボタンのを:

var buttons = $("#yesno button").click(function (e) { 
       var yes = buttons.index(this) === 0; 
       if (yes) { 
        $.ajax({ 
         url: overlayElem.attr('href'), 
         success: function (data) { 
          $("#gridcontainer").html(data); 
         } 
        }); 
       } 
      }); 

削除アクション:

public ActionResult Delete(int id) 
{ 
    DeleteTeamEmployeeInput deleteTeamEmployeeInput = new DeleteTeamEmployeeInput { TeamEmployee = id }; 

    return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput, 
     s => RedirectToAction<EmployeeController>(x => x.Index(1)), 
     f => RedirectToAction<EmployeeController>(x => x.Index(1))); 
} 

問題がありますidパラメータ。直接DeleteTeamEmployeeInputを使用するとよいでしょう。

public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput) 
{ 
    return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput, 
     s => RedirectToAction<EmployeeController>(x => x.Index(1)), 
     f => RedirectToAction<EmployeeController>(x => x.Index(1))); 
} 

私がcomplextオブジェクトを使用すると、常にnullになります。単純なint型はうまく動作します。

削除操作にどのように複雑なタイプを使用できますか?

クラスDeleteTeamEmployeeInput:

public class DeleteTeamEmployeeInput 
{ 
    public int TeamEmployee { get; set; } 
} 

削除ボタン:あなたは間違いなくあなたのクリックコールバック、あるいは時間を持っていない可能性がありますあなたのAJAXリクエストからfalseを返すことで、デフォルトのアクションの結果をキャンセルする必要が

public static string DeleteImageButton(this HtmlHelper helper, int id) 
{ 
    string controller = GetControllerName(helper); 
    string url = String.Format("/{0}/Delete/{1}", controller, id); 

    return ImageButton(helper, url, "Delete", "/Content/Images/application_delete.png", "#yesno", "modalInput", id); 
} 

答えて

1

あなたがリダイレクトされる前に実行する必要があります。限り、あなたがこれを行うことが懸念される(一つだけTeamEmployee整数propertryが含まれている)オブジェクト全体送信など:

<a href="<%: Url.Action("delete", "employee", new { TeamEmployee = "2" }) %>" id="link2" class="modalInput specialbutton" rel="#yesno"> 
    <img src="<%: Url.Content("~/Content/Images/application_delete.png") %>" alt="Delete" /> 
</a> 

:それは、このパラメータを含むように

// that selector seems strange as you don't have a button inside your anchor 
// but an <img>. You probably want to double check selector 
var buttons = $('#yesno button').click(function (e) { 
    var yes = buttons.index(this) === 0; 
    if (yes) { 
     $.ajax({ 
      url: this.href, 
      success: function (data) { 
       $("#gridcontainer").html(data); 
      } 
     // that's what I was talking about canceling the default action 
     }); 
     return false; 
    } 
}); 

をして、あなたのアンカーを生成します今、あなたは安全に持つことができます。

public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput) 
{ 
    ... 
} 

備考:あなたのアンカーでid="2"が有効な識別子名ではありません。

+1

大変ありがとうございました!私が理解しているように、URLにルートデータを追加する必要があります。私の問題は、これを新しい{TeamEmployee = "2"} "ジェネリックにする方法です。私は私のポストを更新します。 – Rookian