Data are not being set in the model ASP.Net mvc

Viewed 69

I have a problem, where I have a form, and some of the data is not being set in the model and I don't know why or where to look.

My Model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace VolunteerMngSystm.Models
{
    public class Organisations
    {
        public int ID { get; set; }
        [Required]
        public string Organisation_name { get; set; }
        [Required]
        public string Industry { get; set; }
        [Required]
        public string Email { get; set; }
        [Required]
        public string OrganisationsID { get; set; }
    }
}

The Email and OrganisationID are not being set.

My Controller Action method:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> OrgReg([Bind("Organisation_name,Industry,Email,OrganisationID")] Organisations organisations)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _context.Add(organisations);
                    await _context.SaveChangesAsync();
                    return RedirectToAction(nameof(OrgHome));
                }
            }
            catch (DbUpdateException e)
            {
                //Log the error (uncomment ex variable name and write a log.
                ModelState.AddModelError("" + e, "Unable to save changes. " +
                    "Try again, and if the problem persists " +
                    "see your system administrator.");
            }

            return View(organisations);
        }

My View:

@model VolunteerMngSystm.Models.Organisations;

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

<h1>Register</h1>

<h5>As Organisation</h5>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="OrgReg">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Organisation_name" class="control-label">Organisation Name</label>
                <input asp-for="Organisation_name" class="form-control" />
                <span asp-validation-for="Organisation_name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Industry" class="control-label"></label>
                <input asp-for="Industry" class="form-control" />
                <span asp-validation-for="Industry" class="text-danger"></span>
            </div>
             <div class="form-group">
                <label asp-for="Email" class="control-label"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="OrganisationsID" class="control-label">Verification ID</label>
                <input asp-for="OrganisationsID" type="file" accept="image/* " />
                <span asp-validation-for="OrganisationsID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Register" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

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

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Can anyone please advise me on where to look or how to fix this problem, please?

1 Answers

Since your action need AntiForgeryToken, add it to your form

 <form asp-action="OrgReg">
 <div asp-validation-summary="ModelOnly" class="text-danger"></div>
@Html.AntiForgeryToken()

or remove it from the action

also try to remove [Bind] from the action. You have a typo there and you don't need it since you are binding all properties any way.

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrgReg( Organisations organisations)
Related