7

現在、HTML送信を実行しようとしていますが、MVCヘルパーメソッドActionLinkを使用して、ボタンにしたくないので、ページの他の部分と同じように下線付きのリンクにします。これは私が現在持っているものです

<%= Html.ActionLink("Delete Selected", "DeleteCheckBox", "Domains", "Default.aspx", new { type="submit" }) %>

これは私のアクションに戻りますが、削除するためにチェックオフされたすべてのドメインは返送されません。(これを使用すると<input type="submit" name="DeleteAction" value="Delete" />、正常に動作するため、チェックボックスの送信または取得に問題がないことがわかります)

これが私がこれまでに持っているものです...

> "%>

索引

<h2>Domain List</h2>

<h2 style="color: #FF0000"><%= Html.Encode(ViewData[IProwlAdminUI.Utils.Global.ExceptionMessageKey]) %></h2>
<h2 style="color: #FF0000"><%= Html.Encode(ViewData["Message"]) %></h2>

<% using (Html.BeginForm("DeleteCheckBox", "Domains"))
   { %>
   <% if (ViewData.ContainsKey("DeleteMessage")) 
       { %>
       <h2 style="color: #FF0000"><%= Html.Encode(ViewData["DeleteMessage"]) %></h2>
       <input type="submit" name="DeleteAction" value="Commit" /> <input type="reset" name="DeleteAction" value="Cancel" /> 
    <% } %>
   <p>
    <%= Html.ActionLink("Create New", "Create") %> 
    | <%= Html.ActionLink("Export List", "Export") %> 
    | **<a href="javascript:void(0)" class="DeleteLink">Delete Selected</a>**

    <% if (ViewData.ContainsKey("Path")) 
       { %> 
       | <%= Html.ReferenceToFile("/download/Domains.xls", "Exported File") %>
    <% } %>
    </p>

    <div style="overflow:scroll; width:100%">
    <% Html.Telerik().Grid(Model).Name("Domains")
        .DataKeys(dataKeys => dataKeys.Add(c => c.DomainId)).DataKeys(dataKeys => dataKeys.Add(c => c.Name))
        .Columns(columns =>
        {
            columns.Template(o =>
            {  %>
                <%= Html.ActionLink("Edit", "Edit", new { id = o.DomainId })%> 
                <%
        }).Title("Edit");
            columns.Template(o =>
            { %>
            <% if (ViewData.ContainsKey("DeleteMessage"))
               { %>
               <input type='checkbox' checked="checked" id='<%= o.Name %>' name='DeleteIds' value='<%= o.DomainId %>' />
            <% } %>
            <% else
                { %>
               <input type='checkbox' id='<%= o.Name %>' name='DeleteIds' value='<%= o.DomainId %>' />
             <% } %>
               <%
        }).Title("Delete");

            columns.Bound(o => o.DomainId);
            columns.Bound(o => o.Name);
            columns.Bound(o => o.SiteId);
            columns.Bound(o => o.ScrubAndRedirect);
            columns.Bound(o => o.ReportingSiteId);
            columns.Bound(o => o.TrafficCopClass);
            columns.Bound(o => o.SiteName);
            columns.Bound(o => o.FeedType);
            columns.Bound(o => o.Active);
        }).Sortable().Filterable().DataBinding(db => db.Server().Select("Index", "Domains")).Render();%>
     </div> 
     <% if (!ViewData.ContainsKey("DeleteMessage"))
        { %>
     <input type="submit" name="DeleteAction" value="Delete" />   
     <% } %>
<% } %>     
<p>
    <%= Html.ActionLink("Create New", "Create") %> | <%= Html.ActionLink("Export List", "Export") %> 
    <% if (ViewData.ContainsKey("Path")) 
       { %> 
       | <%= Html.ReferenceToFile("/download/Domains.xls", "Exported File") %>
    <% } %>
</p>
**<script type="text/javascript">
    $(function() {
        $('.DeleteLink').click(function() {
            $(this).closest('form')[0].submit();
        });
    });
</script>**

4

8 に答える 8

5

Javascriptなしでハイパーリンクにフォームを送信させることはできません。

jQueryを使用すると、次のように記述できます。

<a href="javascript:void(0)" class="DeleteLink">Delete Selected</a>

$('.DeleteLink').click(function() { 
    $(this).closest('form')[0].submit();
});
于 2010-08-10T14:10:01.583 に答える
2

SLak に追加すると、以下を使用して、(ページ上の場所に関係なく) 適切なタイミングで jQuery コードが起動されるようにすることができます。

<script type="text/javascript">
   $(document).ready(function(){
      $('.DeleteLink').click(function() { 
         $(this).closest('form')[0].submit();
       });
   });
</script>

コードをラップ$(document).ready(...)することで、ページの読み込みが完了する前にコードが実行されないようにすることができます。

于 2010-08-11T16:17:37.027 に答える
1

アクションリンクを作成する代わりに、リンクがクリックされたときにフォームを送信するクライアント側のJavaScriptコードを作成することをお勧めします。

フォームを選択するセレクターでsubmitメソッドを使用すると、 jQueryを使用してこれを簡単に行うことができます。

<form id="myForm">
   <!-- Other form inputs -->
   <a id="myFormSubmit" href="#">Submit form</a>
</form>

<script>
    // Assuming you have jQuery loaded.
    $(document).ready(function() {
        // Can load the results of the selector 
        // for the form here.
        // No need to load it every time in the
        // event handler.
        // Even though more often than not the
        // form will cause a reload of the page
        // if you are using the jQuery form validation
        // plugin, you could end up calling the click
        // event repeatedly.
        var myForm = $("#myForm");

        // Add the event handler for the link.
        $("#myFormSubmit").click(function() {
            // Submit the form.
            myForm.submit();

            // Return false, don't want
            // default click action to take place.
            return false;
        });
    });

</script>
于 2010-08-10T14:10:43.107 に答える
1

私が見た回答のほとんどは、jQuery に依存しているか、派手な ajax 送信を行っていますが、これは私が望んでいたものではありません。そこで、非表示の入力とボタンを含むプレーンなフォームを作成する HtmlHelper 拡張メソッドを作成しました。それは進行中の作業です... まだ仕事をすることができます. クラスは次のとおりです。

public static class HtmlHelperExt
{
    public static HtmlString PostLink(this HtmlHelper html, string text, string action, object routeValues)
    {
        var tbForm = new TagBuilder("form");
        tbForm.MergeAttribute("method", "POST");
        tbForm.MergeAttribute("action", action);

        var inputDict = HtmlHelper.ObjectToDictionary(routeValues);
        var inputs = new List<string>();
        foreach (var key in inputDict.Keys)
        {
            const string inputFormat = @"<input type='hidden' name='{0}' value='{1}' />";

            var input = String.Format(inputFormat, key, html.Encode(inputDict[key]));
            inputs.Add(input);
        }

        var submitBtn = "<input type='submit' value='{0}'>";
        inputs.Add(String.Format(submitBtn, text));

        var innerHtml = String.Join("\n", inputs.ToArray());
        tbForm.InnerHtml = innerHtml;

        // return self closing
        return new MvcHtmlString(tbForm.ToString());
    }
}

それを使用するには、Razor で次のように記述します。

@Html.PostLink("ButtonText", "/Controller/Action", new { id = item.Id, hello="world" })

その結果、HTML では次のようになります。

<form action="/Controller/Action" method="POST">
    <input type="hidden" name="id" value="1">
    <input type="hidden" name="hello" value="world">
    <input type="submit" value="ButtonText">
</form>
于 2015-03-19T21:54:27.317 に答える
0

ブートストラップを使用している場合、ボタンをリンクのように見せるには、たとえば btn-link クラスを追加するだけです

<input type="submit" name="ActionType" value="Edit" class="col-md-1 btn btn-link" />
于 2015-07-24T10:53:51.880 に答える
0

これは、C# で javascript からクラスを呼び出すことで実行できます。

 <%= Html.ActionLink("Delete Selected", "DeleteCheckBox", "Domains", "Default.aspx", new { class= "dosubmit" }) %>

Razor 構文の場合

@Html.ActionLink("Delete Selected", "Index", "IndexController", "null", new { @class= "dosubmit" })

次に、Jquery を呼び出して送信します。

<script type="text/javascript">
$(function() {
    $('dosubmit').click(function(e) {
        e.preventDefault();
        $(this).parents('form').first().submit();
    });
});
</script>

更新 1# これをどこで使用できるかについての説明はほとんどありません。

<input type="submit" name="dosubmit" value="Submit something" />

元の質問に行くMVC アクション リンクを作成する 送信を実行する
Index.cshtml の例 chtml ファイルを表示する

@using(Html.BeginForm("Index","ControllerName",FormMethod.Post))
{
   //  Call another view  <a></a> with bootstrap button class
    @Html.ActionLink("Submit something", "Index", "IndexController", "null", new { @class= "dosubmit btn btn-success"  })
}
// Perform a post submit method with same button
<script type="text/javascript">
$(function() {
    $('dosubmit').click(function(e) {
        e.preventDefault();
        $(this).parents('form').first().submit();
    });
});
</script>
于 2016-06-22T20:24:18.110 に答える