0

I have created this C# file to be scaffolded in ASP.NET 5.0. The problem is I do not want the home number to be required yet the generated page (controller with Entity Framework) Create still counts it as such. Any idea why id this happening?

public class Patient
    {
        [Key]
        public string MRN { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string MiddleName { get; set; }
        [Required]
        public string LastName { get; set; }
        [Required]
        public string Address { get; set; }
        [Required]
        public string Gender { get; set; }
        [Required]
        [Display(Name = "Mobile Number")]
        public int MobNumber { get; set; }       
        [Display(Name = "Home Number")]
        public int HomeNumber { get; set; }      
        [Required]
        public string Nationality { get; set; }
            

    }

Here is the code for Create.cshtml, the weird thing that it used to work, now every time I try to generate the controller along with the views, the Home Number is required.

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Patient</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="MRN" class="control-label"></label>
                <input asp-for="MRN" class="form-control" />
                <span asp-validation-for="MRN" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="MiddleName" class="control-label"></label>
                <input asp-for="MiddleName" class="form-control" />
                <span asp-validation-for="MiddleName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Address" class="control-label"></label>
                <input asp-for="Address" class="form-control" />
                <span asp-validation-for="Address" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Gender" class="control-label"></label>
                <input asp-for="Gender" class="form-control" />
                <span asp-validation-for="Gender" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="MobNumber" class="control-label"></label>
                <input asp-for="MobNumber" class="form-control" />
                <span asp-validation-for="MobNumber" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="HomeNumber" class="control-label"></label>
                <input asp-for="HomeNumber" class="form-control" />
                <span asp-validation-for="HomeNumber" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Nationality" class="control-label"></label>
                <input asp-for="Nationality" class="form-control" />
                <span asp-validation-for="Nationality" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
2
  • Can you put the controller code for Create? Commented May 2, 2021 at 6:35
  • As noted below, you need to make your property nullable. However, keep in mind that telephone numbers are not integers. Using int (or int?) is a bad choice. Use a string to store these values and use attributes/data annotations to ensure they are valid Commented May 2, 2021 at 9:20

1 Answer 1

2

If you don't want values to be required, you should make them nullable:

[Display(Name = "Home Number")]
public int? HomeNumber { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.