Search code examples
c#asp.netformsasp.net-corerazor

Null values when sent OnPost() from form ASP.NET Razor Pages


I made a form where i type some data but when the request is sent to OnPost() method it shows me that the values in the property are null. I tried deleting the old database and making a new one, everything worked fine for the first 2 requests and then it happened again. Adds new row but with null values. Thanks!

This is when i try to post

This is when posted

This is my .cshtml file

@page
@using System.Security.Claims
@model AspNetCoreCookieAuthentication.Pages.ClientPanelModel
@{
}

<form method="post">
    <input type="text" asp-for="AppointmentModel.PatientName" name="patientName" placeholder="Name"/>
    <input asp-for="AppointmentModel.Start" asp-format="{0:yyyy-MM-ddTHH:mm}" name="appointmentStart" placeholder="Start" />
    <input asp-for="AppointmentModel.End" asp-format="{0:yyyy-MM-ddTHH:mm}" name="appointmentEnd" placeholder="End" />
    <input type="text" asp-for="AppointmentModel.Description" name="Description" placeholder="Description" />
    <select asp-for="AppointmentModel.DoctorId">
        @foreach(var item in Model.Doctors) {
            <option value="@item.Id">@item.Email</option>
        }
    </select>
    <button type="submit">Submit</button>
</form>



<table class="table">
    <thead>
        <tr>
            <th>Id</th>
            <th>Start</th>
            <th>End</th>
            <th>Patient Name</th>
            <th>Description</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>

    @foreach(var item in Model.Appointments) {
        <tr>
            <td>@item.Id</td>
            <td>@item.Start</td>
            <td>@item.End</td>
            <td>@item.PatientName</td>
            <td>@item.Description</td>
            <td>@item.Status</td>
        </tr>
    }
    </tbody>
</table>

This is the .cshtml.cs file

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using AspNetCoreCookieAuthentication.Data;
using System.Security.Claims;
using System.Data;
using DocSched.Data;

namespace AspNetCoreCookieAuthentication.Pages {
    [Authorize(Policy = "MustBeClient")]
    public class ClientPanelModel : PageModel
    {

    private readonly ApplicationDbContext Db;

    public ClientPanelModel(ApplicationDbContext Db) {
        this.Db = Db;
    }
        public List<User> Doctors { get; set; }

        public List<Appointment> Appointments { get; set; }

        [BindProperty]
        public Appointment AppointmentModel { get; set; }

        public void OnGet() {
            Appointments = Db.Appointments.ToList();
            Doctors = Db.Users.Where(f => f.Role == "Doctor").ToList();
            
        }

        public IActionResult OnPost() {
            var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
            AppointmentModel.PatientId = userId;
            var appointment = AppointmentModel;
            Db.Appointments.Add(appointment);
            Db.SaveChanges();
            return RedirectToPage("Index");
        }
    }
}

Solution

  • Try to remove the name attribute like: name="patientName" , because asp-for will generates the id and name HTML attributes for the expression name specified in the asp-for attribute.

    Modify your code like:

        <input type="text" asp-for="AppointmentModel.PatientName"  placeholder="Name" />
        <input asp-for="AppointmentModel.Start" asp-format="{0:yyyy-MM-ddTHH:mm}" placeholder="Start" />
        <input asp-for="AppointmentModel.End" asp-format="{0:yyyy-MM-ddTHH:mm}"placeholder="End" />
        <input type="text" asp-for="AppointmentModel.Description" placeholder="Description" />
    

    result:

    enter image description here