如何点击按钮将数据从视图发送到控制器
本文关键字:控制器 视图 何点击 按钮 数据 | 更新日期: 2023-09-27 18:19:35
单击按钮时,如何调用控制器操作并及时发送下拉列表中选择的值?下面是我的.cs.html的外观示例。这只是一个例子,通常我需要在点击按钮时及时从当前视图中收集大量数据。
<body>
<div>
@Html.DropDownList("Name")
<br />
@Html.DropDownList("Age")
<br />
@Html.DropDownList("Gender")
<br />
@using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post))
{
<input type="submit" value="Find" />
}
</div>
</body>
为了将数据提交给控制器,输入必须出现在<form>
标记中。
例如:
<body>
<div>
@using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post))
{
@Html.DropDownList("Name")
<br />
@Html.DropDownList("Age")
<br />
@Html.DropDownList("Gender")
<br />
<input type="submit" value="Find" />
}
</div>
</body>
在@using(Html.BeginForm("FindPerson","MyController",FormMethod.Post))中,您应该输入。
您的输入在Form 之外
@using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post))
{
@Html.DropDownList("Name")
<br />
@Html.DropDownList("Age")
<br />
@Html.DropDownList("Gender")
<br />
<input type="submit" value="Find" />
}
首先需要Model来绑定数据。
public class TestModel
{
public string Age { get; set; }
public string Gender { get; set; }
...
}
然后你需要将你的下拉列表包装在表单标签中
<form method='post'>
@Html.DropDownList("Age")
</form>
以及接收公布数据的行动
[HttpPost]
public ActionResult YourAction(TestModel model)//selected data here
{
}
在@using(Html.BeginForm("NameOfActionMethod","ControllerName",FormMethod.Post))中,您应该输入。
您的输入在Form 之外
@using (Html.BeginForm("NameOfActionMethod", "ControllerName", FormMethod.Post))
{
<input type="submit" value="Find" />
}