Request.Form.GetValues在ASP中不起作用.净的核心



这里有一段代码:

if (Request.Form.GetValues("multipleTags") is not null)
selectedTags = Request.Form.GetValues("multipleTags").ToList();

我如何在。net 5中实现这个?

错误我是没有GetValues方法可用。

使用mvc时,我同意@mxmissile在视图中绑定模型并在控制器中通过模型接收数据。一些细节在这个官方文件。

在你的问题旁边,我在我的视图中设置了一个表单,像这样:

@{
}
<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方法时,我们从querystring(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;
}
}
}

最新更新