Request.Form.GetValues 不适用于 ASP.NET 核心
Request.Form.GetValues does not work in ASP.NET Core
我这里有这段代码:
if (Request.Form.GetValues("multipleTags") is not null)
selectedTags = Request.Form.GetValues("multipleTags").ToList();
如何在 .NET 5 中实现它?
我收到的错误是没有可用的 GetValues
方法。
使用 mvc 时,我同意@mxmissile 在视图中绑定模型并在控制器中按模型接收数据。 official document.
中的一些细节
除了你的问题,我在我的视图中设置了这样一个表单:
@{
}
<form id="myForm" action="hello/getForm" method="post">
Firstname: <input type="text" name="firstname" size="20"><br />
Lastname: <input type="text" name="lastname" size="20"><br />
<br />
<input type="button" onclick="formSubmit()" value="Submit">
</form>
<script type="text/javascript">
function formSubmit() {
document.getElementById("myForm").submit()
}
</script>
这是我的控制器方法,当使用 get 方法时,我们从查询字符串(Request.Query
)收集数据,而使用 post 方法我们在请求正文(Request.Form
)中获取数据:
using Microsoft.AspNetCore.Mvc;
namespace WebMvcApp.Controllers
{
public class HelloController : Controller
{
public IActionResult Index()
{
return View();
}
public string getForm() {
var a = HttpContext.Request.Query["firstname"].ToString();
var b = HttpContext.Request.Query["lastname"].ToString();
var c = HttpContext.Request.Query["xxx"].ToString();
var d = HttpContext.Request.Form["firstname"].ToString();
var e = HttpContext.Request.Form["lastname"].ToString();
var f = HttpContext.Request.Form["xxx"].ToString();
return "a is:" + a + " b is:" + b + " c is:" + c +" d is:"+d+" e is"+e+" f is:"+f;
}
}
}
我这里有这段代码:
if (Request.Form.GetValues("multipleTags") is not null)
selectedTags = Request.Form.GetValues("multipleTags").ToList();
如何在 .NET 5 中实现它?
我收到的错误是没有可用的 GetValues
方法。
使用 mvc 时,我同意@mxmissile 在视图中绑定模型并在控制器中按模型接收数据。 official document.
中的一些细节除了你的问题,我在我的视图中设置了这样一个表单:
@{
}
<form id="myForm" action="hello/getForm" method="post">
Firstname: <input type="text" name="firstname" size="20"><br />
Lastname: <input type="text" name="lastname" size="20"><br />
<br />
<input type="button" onclick="formSubmit()" value="Submit">
</form>
<script type="text/javascript">
function formSubmit() {
document.getElementById("myForm").submit()
}
</script>
这是我的控制器方法,当使用 get 方法时,我们从查询字符串(Request.Query
)收集数据,而使用 post 方法我们在请求正文(Request.Form
)中获取数据:
using Microsoft.AspNetCore.Mvc;
namespace WebMvcApp.Controllers
{
public class HelloController : Controller
{
public IActionResult Index()
{
return View();
}
public string getForm() {
var a = HttpContext.Request.Query["firstname"].ToString();
var b = HttpContext.Request.Query["lastname"].ToString();
var c = HttpContext.Request.Query["xxx"].ToString();
var d = HttpContext.Request.Form["firstname"].ToString();
var e = HttpContext.Request.Form["lastname"].ToString();
var f = HttpContext.Request.Form["xxx"].ToString();
return "a is:" + a + " b is:" + b + " c is:" + c +" d is:"+d+" e is"+e+" f is:"+f;
}
}
}