您可以为每个按钮创建一个单独的表单:
<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.js
Ajax.ActionLink