2

I've got this piece of code here :

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

How do I implement this in .NET 5?

The error I'm getting is that there is no GetValues method available.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kosta
  • 43
  • 1
  • 6
  • Does this answer your question? [HttpContext.Current.Request.Form.AllKeys in ASP.NET CORE version](https://stackoverflow.com/questions/43284562/httpcontext-current-request-form-allkeys-in-asp-net-core-version) – CoolBots Oct 12 '21 at 15:02
  • 1
    You are better off using `Request.Form.TryGetValue` anyway, much safer and more readable. – DavidG Oct 12 '21 at 15:05
  • Or even better, use a model and let binding do the work. – mxmissile Oct 12 '21 at 15:50
  • How is it going on sir? Pls feel free to share your further issue if existing. And could you pls accept my post as the answer if you feel my post is helpful to you to end this case ? Thanks in advance for your reply :) – Tiny Wang Oct 15 '21 at 02:52

1 Answers1

3

When using mvc, I agree with @mxmissile to bind a model in the view and receiving data by model in your controller. Some details in this official document.

And alongside your question, I set a form in my view like this:

@{
}

<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>

And this is my controller method, when using get method, we gather data from querystring(Request.Query) while using post method we get data in request body(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;
        }
    }
}
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29