0

I've tried submit a simple Form but I got this error:

"Sending Data Status Code: 404; Not Found"

I've used ASP.NET Core 2 and I'm new in it!

How can I solve this problem?

a part of Startup class:

 routes.MapRoute(
                name: "ForTest",
                template: "{controller}/{action}/{des}",
                defaults: new { controller = "Comment", action = "Test", des = "no comment!" }
            );

a part of cshtml file:

<form method="post" asp-controller="Comment" asp-action="Test"  enctype="multipart/form-data">
    <div class="form-group">
      <textarea id="des" class="form-control" rows="3"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

a part of CommentController class:

[Route("[controller]")]
public class CommentController : Controller
{
    private readonly ApplicationDbContext _context;

    public CommentController(ApplicationDbContext context)
    {
        _context = context;
    }

    [HttpPost]
    [Route("Test/{des}")]
    [ValidateAntiForgeryToken]
    public IActionResult Test(string des)
    {
        return null;
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
x19
  • 8,277
  • 15
  • 68
  • 126
  • Where is that error coming from exactly? It's not a standard framework message. – DavidG Jan 23 '18 at 23:33
  • 1
    Your `ForTest` route looks suspicious. If you still have your `Default` route in the project, [your routing is misconfigured](https://stackoverflow.com/a/35674633/). – NightOwl888 Jan 23 '18 at 23:58

1 Answers1

1

I would recommend using asp-for attribute on textarea and use ViewModel class.
Also, fix Route attributes.

Controller / Model

public class TestViewModel
{
    public string des { get; set; }
}

public class CommentController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Test(TestViewModel testViewModel)
    {
        return Ok(testViewModel.des);
    }
}

cshtml

@using @*Set proper namespace for TestViewModel*@
@model TestViewModel

<form method="post" asp-controller="Comment" asp-action="Test" enctype="multipart/form-data">
    <div class="form-group">
        <textarea asp-for="des" rows="3"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

enter image description here

idubnori
  • 997
  • 9
  • 21