Home > Software engineering >  Model Binding is forcing to have value in non-required field while posting the form in .NET Core MVC
Model Binding is forcing to have value in non-required field while posting the form in .NET Core MVC

Time:02-23

I am posting value from MVC View.. Below Is .cshtml Code

 <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" ></div>
            <div >
                <label asp-for="Name" ></label>
                <input asp-for="Name"  />
                <span asp-validation-for="Name" ></span>
            </div>
            <div >
                <label asp-for="ContractStartDate" ></label>
                <input asp-for="ContractStartDate"  />
                <span asp-validation-for="ContractStartDate" ></span>
            </div>
            <div >
                <label asp-for="ContractEndDate" ></label>
                <input asp-for="ContractEndDate"  />
                <span asp-validation-for="ContractEndDate" ></span>
            </div>
            <div >
                <label >
                    <input  asp-for="IsActive" /> @Html.DisplayNameFor(model => model.IsActive)
                </label>
            </div>
            <div >
                <label asp-for="Website" ></label>
                <input asp-for="Website"  />
                <span asp-validation-for="Website" ></span>
            </div>
            <div >
                <label asp-for="LogoUrl" ></label>
                <input asp-for="LogoUrl"  />
                <span asp-validation-for="LogoUrl" ></span>
            </div>
            <div >
                <input type="submit" value="Create"  />
            </div>
        </form>

This is my Model Code

public class Client : BaseEntity
    {
        [Key]
        public int Id { get; set; }

        [Required(ErrorMessage ="Client Name is Required")]
        [Display(Name ="Client Name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Contract StartDate is Required")]
        [DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]
        [Display(Name = "Contract StartDate")]
        public DateTime ContractStartDate { get; set; }

        [Required(ErrorMessage = "Contract EndDate is Required")]
        [DataType(DataType.Date, ErrorMessage = "Invalid Date Format")]
        [Display(Name = "Contract End Date")]
        public DateTime ContractEndDate { get; set; }

        [Required]
        [Display(Name = "Is Active")]
        public bool IsActive { get; set; }

        [Required]
        public string Website { get; set; }

        public string LogoUrl { get; set; }
    }

BaseEntity.cs Code

public abstract class BaseEntity
    {
        public string CreatedBy { get; set; }
        public DateTime CreatedDate { get; set; }
        public string CreatedIPAddress { get; set; }

        public string ModifiedBy { get; set; }
        public DateTime ModifiedDate { get; set; }
        public string ModifiedIPAddress { get; set; }
    }

This is Post function in MVC Controller..

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,ContractStartDate,ContractEndDate,IsActive,Website,LogoUrl")] Client client)
        {
            if (ModelState.IsValid)
            {
                _context.Add(client);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(client);
        }

When I click on submit button after providing required values... it shows validation error even for non-required filed of BaseEntity.cs class.

enter image description here

When I fill all these non-required field and remove from post Bind[] method of controller... There it is showing Model is invalid.

enter image description here

Controller Model Validation

enter image description here

See Detailed error...

enter image description here

Please help.... how to bypass this error.

CodePudding user response:

Since you are using net 6 you have 2 choices

  1. Remove or comment this line in EF core project
 <!--<Nullable>enable</Nullable>-->
  1. Or manually make each property of each class nullable
       public string? CreatedBy { get; set; }
        public DateTime? CreatedDate { get; set; }
        public string? CreatedIPAddress { get; set; }
      // ---and so on for all classes
  • Related