1

我想对一个按钮采取一些行动,点击下面给出的代码--->

<% if(Model!=null && Model.Count>0){%>
<ul>
<% foreach(var Movie in Model)
        {
            %> <li> <%= Movie.Id %>  
            <%= Movie.Title %>  
            <%= Movie.ReleaseDate %> 

            <input type="button" value="edit" name="edit" onclick="EditMovie" /> 
            <input type="button" value="delete" name="delete" /> </li> 
            <% } %>
<% } %>
</ul>

我有一些功能要做,但是如何将这些按钮链接到这些控件?

4

5 回答 5

2

您可以为每个按钮创建一个单独的表单:

<ul>
    <% foreach(var Movie in Model) { %> 
    <li> 
        <%: Movie.Id %>  
        <%: Movie.Title %>  
        <%: Movie.ReleaseDate %> 
        <%= Html.BeginForm("EditMovie", "Movies", new { id = Movie.Id }, FormMethod.Post) { %>
            <input type="submit" value="edit" name="edit" /> 
        <% } %>
        <%= Html.BeginForm("DeleteMovie", "Movies", new { id = Movie.Id }, FormMethod.Post) { %>
            <input type="submit" value="delete" name="delete" /> 
        <% } %>
    </li> 
    <% } %>
</ul>

然后您将有 2 个相应的控制器操作:

[HttpPost]
public ActionResult EditMovie(int id)
{
    ...
}

[HttpPost]
public ActionResult DeleteMovie(int id)
{
    ...
}

作为使用表单的替代方法,您可以使用 AJAX:

<ul>
    <% foreach(var Movie in Model) { %> 
    <li> 
        <%: Movie.Id %>  
        <%: Movie.Title %>  
        <%: Movie.ReleaseDate %> 

        <%= Ajax.ActionLink(
            "edit", 
            "EditMovie", 
            "Movies", 
            new { id = Movie.Id }, 
            new AjaxOptions { HttpMethod = "POST" }
        ) %>

        <%= Ajax.ActionLink(
            "delete", 
            "DeleteMovie", 
            "Movies", 
            new { id = Movie.Id }, 
            new AjaxOptions { HttpMethod = "POST" }
        ) %>
    </li> 
    <% } %>
</ul>

如果您决定使用帮助程序,请不要忘记在您的页面中包含jquery和脚本。jquery.unobtrusive-ajax.jsAjax.ActionLink

于 2013-01-04T13:05:16.370 回答
0

最简单的方法?像这样的东西:

<% if(Model!=null && Model.Count>0){%>
<ul>
    <% foreach(var Movie in Model) { %> 
        <li>
            <%= Movie.Id %>  
            <%= Movie.Title %>  
            <%= Movie.ReleaseDate %> 
            <% using (Html.BeginForm("EditMovie")) { %>
                <%: Html.Hidden("Id", Movie.Id) %>
                <input type="submit" value="edit" name="edit" /> 
                <input type="submit" value="delete" name="delete" /> 
            <% } %>
        </li> 
    <% } %>
</ul>
<% } %>

然后在你的代码后面:

public ActionResult EditMovie(int Id, string edit, string delete)
{
    if (delete != null)
    {
        // delete 
    }

    if (edit != null)
    {
        // edit
    }

    return View();
}
于 2013-01-04T13:08:19.070 回答
0

连接处理程序的 jQuery 类似于;

    $(document).ready(function(){
    $('input[type="button"]').on('click', function(e){
        // etc
    });
});
于 2013-01-04T13:13:06.493 回答
0
You can handle form action with Jquery.

$(document).ready(function(){
    $('.button-edit').on('click', function(e){
        $('#formMovie').attr('action','/edit/'+ someParameter)
    });
    $('.button-delete').on('click', function(e){
        $('#formMovie').attr('action','/edit/' + someParameter)
    });
});
于 2013-01-04T14:08:32.173 回答
0
    foreach {
...
        <button type="button" class="btn btn-default editButton" id="movie_@Movie.Id">Edit</button>
...
    }

@section Scripts {
    <script type="text/javascript">
    $(document).ready(function () {
        $("editButton").click(function () {
            var id = this.id.split("_")[0];
            location.href = '@Url.Action("EditMovie", new RouteValueDictionary() { { "Controller", "Movie" } } )?id=' + id;
        });
    });
    </script>
}
于 2013-10-25T12:16:58.050 回答