asp.net-core-mvc

Data returns null


enter image description here

I'm writing a "Course" application in ASP.NET Core MVC. When I try to apply any course, it returns no value for SelectedCourse. I tried

<input id "mvc1" type="radio" asp-for="SelectedCourse"value="ASP.NET Core MVC" checked="true" />

but it didn't work.

Apply.cshtml:

<div>
    <fieldset></fieldset>

    <legend>Select One(1) Course</legend>
    <p>
        <input id "mvc1" type="radio" asp-for="SelectedCourse"value="ASP.NET Core MVC" checked="true" />
        <label>ASP.NET Core MVC</label>
    </p>
    <p>
        <input id "mvc2" type="radio" asp-for="SelectedCourse" value="Blazor" />
        <label>Blazor</label>
    </p>
    <p>
        <input id "mvc3" type="radio" asp-for="SelectedCourse"value="API" />
        <label>Api</label>
    </p>
</div>

Repository.cs:

namespace BTK_Akademi.Models
{
    public static class Repository
    {
        private static List<Candidate> applications = new();
        //Listenin dışardan düzenlenmemesi lazım yani readonly olacak 
        public static IEnumerable<Candidate> Applications => applications;
        
        public static void Add(Candidate candidate)
        {
            applications.Add(candidate); 
        }
    }
}

Solution

  • I'm writing a "Course" application in ASP.NET Core MVC. When I try to apply any course, it returns no value for SelectedCourse.

    Based on your shared code, it might be because of your model initialization in view. If asp-for="SelectedCourse" has not been defined while you are loading the view, then during submit the form it might not bind with your Apply(Candidate candidate) of [HttpPost] method within controller that can cause null data.

    In addition, remember it should be within <form asp-action="Apply" method="post"> which you are missing.

    So, basically your model has not been binded accordingly.

    Let have a look, how should you do that correctly:

    Model:

    public class Candidate
    {
        public string SelectedCourse { get; set; }
    }
    

    Load View:

    public IActionResult Apply()
     {
         return View(new Candidate());
     }
    

    View:

    @model CustomerManagementTool.Models.Candidate
    
    @{
        ViewData["Title"] = "Apply";
    }
    
    <h2>@ViewData["Title"]</h2>
    
    <form asp-action="Apply" method="post">
        <div>
            <fieldset>
                <legend>Select One(1) Course</legend>
                <p>
                    <input id="mvc1" type="radio" asp-for="SelectedCourse" value="ASP.NET Core MVC" checked="true" />
                    <label for="mvc1">ASP.NET Core MVC</label>
                </p>
                <p>
                    <input id="mvc2" type="radio" asp-for="SelectedCourse" value="Blazor" />
                    <label for="mvc2">Blazor</label>
                </p>
                <p>
                    <input id="mvc3" type="radio" asp-for="SelectedCourse" value="API" />
                    <label for="mvc3">API</label>
                </p>
            </fieldset>
        </div>
        <button type="submit">Apply</button>
    </form>
    

    Action Method form submit:

            [HttpPost]
            public IActionResult Apply(Candidate candidate)
            {
                if (ModelState.IsValid)
                {
                    Repository.Add(candidate);
                    return RedirectToAction("Index");//You can redirect where you need
                }
    
                return View(candidate);
            }
    

    Output:

    enter image description here

    enter image description here

    enter image description here