locked
Unable to route through Area RRS feed

  • Question

  • User-1256377279 posted

    HI All,

    Please can anyone help using Area Routing which is not working using ASP.NET Core 3.1

    HTML Code

    <div class="row">
        <div class="col-12">
            @Html.DropDownList("NationCase", (SelectList)ViewBag.NationList, "Select nation", new
    { onchange = "document.location.href =  '/' + this.options[this.selectedIndex].value;", @class = "form-control form-control-sm col-sm-3 mb-1", @style = "float:left;" })
    
        </div>
    </div

    C#

      [Area("Admin")]
        public class WebController : Controller
        {
    
            private readonly GlobalDBContext _context;
    
    
            public WebController(GlobalDBContextcontext)
            {
                _context = context;            
            }
    
            public IActionResult Index()
            {
                getNationList(null);
                return View();
            }
    
    
            [Route("/{id}")]
            public IActionResult Index(int id)
            {
                
               
                ViewBag.LocID = id;
                getNationList(id);
                return View();
            }
    
    
    
    
            private void getNationList(int? id)
            {
                var nationList = _context.NationMeterTop.Select(s => new { Id = s.LocationId, Value = s.Nation }).OrderBy(s => s.Value).ToList();
                ViewBag.NationList = new SelectList(nationList, "Id", "Value", id);
            }
        }

    Startup.cs

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

    I want when user select value from dropdown it should below action method

    [Route("/{id}")]
            public IActionResult Index(int id)
            {
                
               
                ViewBag.LocID = id;
                getNationList(id);
                return View();
            }

    Thanks,

    Shabbir

    Friday, May 15, 2020 3:53 PM

All replies

  • User-854763662 posted

    Hi shabbir_215 ,

    I tested your code on ASP.NET Core 3.1 project , but it worked well. The Index action with id prarmeter is called after selecting the value from dropdown.

    Could  you share the demo link that can reproduce the issue so that we can provide more help for you?

    Best Regards,

    Sherry

    Saturday, May 16, 2020 2:59 AM