0

私はMVC3アプリケーションに取り組んでいます。私のビューには、コントローラーに移動したいロジックがあります。モデルの特定の側面に応じて、ActionLink を動的に表示します。異なるのは、Html.ActionLink の linkText パラメーターと actionName パラメーターだけです。linkText と actionName の文字列を含む JsonResult を返すメソッドをコントローラーに追加しました。

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetActionButton(int id)
{
    string action = null;
    string text = null;

    // Snipped stuff that sets action and text

    return Json(new
    {
        buttonAction = action,
        buttonText = text
    });
}

結果を使用してリンクを作成するには、このメソッドをどこで呼び出すことができますか?

4

3 に答える 3

2

コントローラーからのリンク生成については、UrlHelper MethodsActionを参照してください。通常の URL を受け取るために使用することをお勧めします。jquery を使用するクライアントでは、次のようなリンクを作成できます。

$('<a>').attr('href', data.buttonAction).text(data.buttonText)
于 2012-09-24T19:18:02.387 に答える
1

HTMLヘルパーが必要だと思います。

    public static MvcHtmlString MyActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
    {
        string actionLinkHtml = string.Format("<a href=\"/{0}/{1}\">{2}</a>", controllerName, actionName, linkText);
        return new MvcHtmlString(actionLinkHtml);
    }

もちろん、変数を設定するためのコードがさらにあり、パラメーターはおそらく上記と同じ int ID になりますが、これが基本的な考え方です。

次に、ビューで次を使用します。

    @Html.MyActionLink("link text", "action", "ctrlr")
于 2012-09-24T19:12:35.040 に答える
1

ビューモデルで推測しているように、複数のIDはどこから来ているのですか?

おそらく、ドキュメントの準備が整った状態で jQuery/AJAX を使用します。

$(document).ready(function() {
  // Handler for .ready() called.
  // AJAX call to GetActionButton for each enumeration over the ID's in the view model
});
于 2012-09-24T18:56:48.733 に答える