0

My job is to make a website for a school management system. I have students and admins who can log in the system. Students can register. I have created controller for ASP.NET MVC 5 using template for Entity Framework. It have created Account model, Controller and Create, Delete, Details, Edit, Index views. Fields of account are:

public int AccountID { get; set; }
public Nullable<int> TypeID { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ICNumber { get; set; }
public string Telephone { get; set; }
public string email { get; set; }
public string Adress { get; set; }`

I have changed my views to hide information that does not need to shown, as ID, password etc. (My code =>)

@{
ViewBag.Title = "Register";
}
@model WebApplication9.Account


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Account</h4>
    <hr />

    <div class="form-group">
        @Html.LabelFor(model => model.TypeID, "TypeID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("TypeID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.TypeID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ICNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ICNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ICNumber, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Adress, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Adress, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Adress, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

Account ID should be autogenereted in database, I have also used Identity as StoreGenerated Pattern in Account Model but Im getting error while trying to registrate new student:

SQLException: Cannot insert the value NULL into column 'AccountID'

Controller code:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register([Bind(Include = "AccountID,TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
    {
        if (ModelState.IsValid)
        {
            db.Accounts.Add(account);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.TypeID = new SelectList(db.AccountTypes, "TypeID", "TypeName", account.TypeID);
        return View(account);
    }
8
  • Try putting the data attribute [Key] above your AccountID property in your Account model. Also would be helpful to know if you are using Code First or Database First. Commented Jun 20, 2017 at 17:04
  • On another note, not sure how far into development you are but ASP.NET MVC 5 has Identity and it is a great way to start off with user accounts and authentication. Does a lot of the stuff for you and you don't have to worry about all the nuances of authentication like salting and hashing passwords. Commented Jun 20, 2017 at 17:06
  • @David Lee Database First Commented Jun 20, 2017 at 17:07
  • Try adding the [Key] attribute and make sure your database is setup as an auto incremented field. You could also remove the AccountID from your [Bind(Include = "")] as this is not being set by the post and being managed on the server. Commented Jun 20, 2017 at 17:09
  • @DavidLee I know about it, but have problems with connecting it to my own database, so I decided to make my own verification, and everything work fine except registration. Commented Jun 20, 2017 at 17:09

1 Answer 1

1

Accout Model

Try adding the [Key] attribute and make sure your database is setup as an auto incremented field.

[Key]
public int AccountID { get; set; }

Controller

You could also remove the AccountID from your [Bind(Include = "")] as this is not being set by the post and being managed on the server. This should not be causing the error, but would be good practice to do.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "TypeID,Password,FirstName,LastName,ICNumber,Telephone,email,Adress")] Account account)
{
    // stuff
}

SQL Server

On your SQL Server the AccountID should be set to Yes for Identity Specification and (Is Identity) should be Yes. Identity Increment is usually 1 and identity seed is usually 1 but those should not be causing any issues with your insert.

Sign up to request clarification or add additional context in comments.

2 Comments

It helped thanks! But one more problem I found all models are overwritten after I made some changes in Model.edmx... Is there any way to prevent it?
You are welcome, I posted in the comment to your question about the model overwrites. Not sure if I can help you there.

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.