Home > other >  ASP.NET Routes config not following tutorial
ASP.NET Routes config not following tutorial

Time:12-30

I'm trying to follow a basic tutorial to get forms working: https://www.completecsharptutorial.com/asp-net-mvc5/4-ways-to-create-form-in-asp-net-mvc.php

After running the first example my code trying to render localhost:5001/form1 instead of the example's localhost:5001/Home/form1

I'm assuming the issue is my Starup.cs routes needs to be tweaked. It currently looks like:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

Controller code:

namespace test_ops.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = 
ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = 
Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        [HttpPost] 
        public ActionResult form1(int txtId, string txtName, string chkAddon)
        {
            ViewBag.Id = txtId;
            ViewBag.Name = txtName;
            if (chkAddon != null)
                ViewBag.Addon = "Selected";
            else
                ViewBag.Addon = "Not Selected";

            return View("Index");
        }
    }
}

Model code:

namespace test_ops.Models
{
    public class TestModel
    {
        [Required]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Addon { get; set; }
    }
}

View code in Home/index.cshtml:

<h4 style="color:purple">
<b>ID:</b>    @ViewBag.ID <br />
<b>Name:</b>  @ViewBag.Name <br />
<b>Addon:</b> @ViewBag.Addon
</h4>
<hr />
<h3><b>Forms: Weakly Typed</b></h3>

<form action="form1" method="post">
    <table>
        <tr>
            <td>Enter ID: </td>
            <td><input type="text" name="txtId" /></td>
        </tr>
        <tr>
            <td>Enter Name: </td>
            <td><input type="text" name="txtName" /></td>
        </tr>
        <tr>
            <td>Addon: </td>
            <td><input type="checkbox" name="chkAddon" /></td>
        </tr>
        <tr>
                <td colspan="2"><input type="submit" value="Submit Form" /></td>
        </tr>
    </table>
</form>

After submitting the form the page comes back with the a 404: "This localhost page can’t be foundNo webpage was found for the web address: https://localhost:5001/form1 HTTP ERROR 404"

Can someone please let me know what needs to be changed to get this working?

CodePudding user response:

try to add controller to your form

<form action="@Url.Action("form1", "home")" method="post">
...

but is much better to use this syntax instead of form

@using (Html.BeginForm("form1", "Home", FormMethod.Post))
{   
 <table>
        <tr>
    //...    
    
  <tr>
   <td colspan="2"><input type="submit" value="Submit Form" /></td>
        </tr>
}

and check a file _ViewImports.cshtml in a View folder. It should have

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  • Related