我有一个包含项目列表的 View 。每个项目都有一个文本框和一个按钮。 获取 Controller 操作中单击的按钮的项目 id 的最佳方法是什么? 我需要 Controller 操作中关联文本框中的值,因此我认为我无法使用操作链接。
最佳答案
有多种方法可以做到这一点。有些使用 JavaScript,有些则不使用。我个人更喜欢不使用 javascript 来实现基本功能,除非您的设计本身就是基于 javascript 的(例如使用 ajax)
例如,您可以将每个项目包装在它自己的表单中,并使用不同的提交值。请注意不要嵌套表单,因为那不是有效的 HTML。
例如:
@using(Html.BeginForm("MyAction", "MyController", new { id=1 })) {
<input type="submit"/>
@Html.TextBox("TheValue", "One")
}
@using(Html.BeginForm("MyAction", "MyController", new { id=2 })) {
<input type="submit"/>
@Html.TextBox("TheValue", "Two")
}
public ActionResult MyAction(int? id, string TheValue) {
// if they click the first one, id is 1, TheValue = "One"
// if they click the second one, id is 2, TheValue = "Two"
}
关于c# - MVC 表单上的多个按钮 - 在操作 Controller 中获取项目 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206793/