0

文字列値をアイテムの作成ダイアログに渡そうとしていますが、その方法がわかりません。

これは私の見解のコードです:

JavaScript:

function newRoute() {
    $.ajax({
        url: '@Url.Action("Create")',
        success: function (data) {
            if (data == "success") //successfully created the new route
                window.location.href = '@Url.RouteUrl(ViewContext.RouteData.Values)'
            else
                $.facybox(data); // there are validation errors, show the dialog w/ the errors
        }
    });
}

意見:

<td>@route</td>
<td>
    <a href="javascript:newRoute();" class="button">Add</a>
</td>

コントローラー

public ActionResult Create(string routeName = "")
    {
        PopulateRouteInfoViewBag();

        var newRoute = new RouteInformation();

        newRoute.Name = routeName;

        return View(newRoute);
    }

@routeの値を取得し、それをCreateコントローラーに渡して、渡された文字列値を使用してダイアログをポップアップ表示しようとしています。

4

2 に答える 2

0

2 つのオプションがあります。1、使用Url.Action("controllerName", "actionName", new {routeName = "your route name here"})または 2、$.ajax に渡されたオブジェクトのデータ プロパティを使用します。

2 の場合、JavaScript は次のようになります

function newRoute() {
    $.ajax({
        url: '@Url.Action("Create")',
        data: {
            route: "your data here"
        }
        success: function (data) {
            if (data == "success") //successfully created the new route
                window.location.href = '@Url.RouteUrl(ViewContext.RouteData.Values)'
            else
                $.facybox(data); // there are validation errors, show the dialog w/ the errors
        }
    });
}
于 2013-01-14T18:34:55.650 に答える
0

ActionLink html ヘルパー メソッドを使用して、次のようにルート変数を渡します。

@{
   string route="somevaluehere";
 }
@Html.ActionLink("Add","Create","YourControllerName",
                          new { routeName=route},new {@id="addLnk"})

クリックイベントを処理する

$(function(){

   $("#addLnk").click(function(e){
     e.preventDefault();  //prevent normal link click behaviour
     var _this=$(this); 

      //do your ajax call now
      $.ajax({
        url: _this.attr("href"),
        success: function (data) {
            if (data == "success") //successfully created the new route
                window.location.href = 'someValidUrlHere'
            else
                $.facybox(data); 
         }
       });

   });     

});

また、新しいページへのパス (アクション メソッド) を作成し、それを JSON 結果の一部として返し、クライアントに JSON から読み取らせることを検討することもできます。

ルート変数の値をクエリ文字列に追加する代わりに、それをメッセージ本文の一部と見なすことができます。

于 2013-01-14T18:34:20.857 に答える