在asp.net mvc中插入后表不递增

时间:2017-07-17 18:08:02

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在从事ASP.NET MVC项目。我创建了各种模型和在我的项目中使用的视图模型。我也用种子数据播种了我的数据库表,但在实现注册视图时,我测试了表单,但是当数值插入到数据库表的Id部分时,它变为0。我截断了表并做了一个新的插入,我仍然有同样的错误。

以下是用户表的模型

public class User
{
    [Key]
    public byte Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string RefIndicator { get; set; }
    public Team TeamCategory { get; set; }
    public byte TeamId { get; set; }
    public bool IsRegistered { get; set; }
    public DateTime DateRegistered { get; set; }
    public DateTime? LastModified { get; set; }
    public UserRoles UserRoles { get; set; }
    public byte UserRolesId { get; set; }
}

以下是我为我需要在视图中使用的Team Model属性创建的viewModel

public class RegisterFormViewModel
{
    public User Users { get; set; }
    public byte Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public IEnumerable<Team> Teams { get; set; }
    public byte TeamId { get; set; }
    public string RefIndicator { get; set; }
    public bool IsRegistered { get; set; }
    public DateTime DateRegistered { get; set; }
    public byte UserRolesId { get; set; }
}

这是userController的register动作,用于初始化Register视图的值

public ActionResult Register()
{
    var AppUser = User.Identity.Name.Substring(5);
    var AppUserEmail = AppUser + "@nlng.com";
    int index = AppUser.IndexOf('.');
    var FirstName = AppUser.Substring(0, index);
    var LastName = AppUser.Substring(index + 1);
    var IsRegistered = true;
    var UserRolesId = 1;
    var DateRegistered = HttpContext.Timestamp;

    var teams = _context.Team.ToList();

    var viewModel = new RegisterFormViewModel{
        Email = AppUserEmail,
        FirstName = FirstName,
        LastName = LastName,
        Teams = _context.Team.ToList(),
        IsRegistered = IsRegistered,
        UserRolesId = (byte)UserRolesId,
        DateRegistered = DateRegistered               
    };

    return View("Register", viewModel);
}

最后这里是注册页面的相关视图

@model eLeave.ViewModel.RegisterFormViewModel
@{
    ViewBag.Title = "Register";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>User Registration</h2>

@using (Html.BeginForm("Save", "User"))
{
    <div class="form-group">
        @Html.LabelFor(r=>r.FirstName)
        @Html.TextBoxFor(r => r.FirstName, new { @class = "form-control", @readonly = true })
    </div>

    <div class="form-group">
        @Html.LabelFor(r => r.LastName)
        @Html.TextBoxFor(r => r.LastName, new { @class = "form-control", @readonly = true })
    </div>

    <div class="form-group">
        @Html.LabelFor(r => r.Email)
        @Html.TextBoxFor(r => r.Email, new { @class = "form-control", @readonly = true })
    </div>

    <div class="form-group">
        @Html.LabelFor(r => r.TeamId)
        @Html.DropDownListFor(r => r.TeamId, new SelectList(Model.Teams, "Id", "TeamName"), "Select your Team", new { @class = "form-control" })
    </div>

    <div class="form-group">
        @Html.LabelFor(r => r.RefIndicator)
        @Html.TextBoxFor(r => r.RefIndicator, new { @class = "form-control" })
    </div>

    @Html.HiddenFor(m => m.IsRegistered)
    @Html.HiddenFor(m => m.DateRegistered)
    @Html.HiddenFor(m => m.UserRolesId)
    @Html.HiddenFor(m => m.Id)
@Html.AntiForgeryToken()
        <button class="btn btn-primary">Register</button>
    }

最后,这是userController的保存操作

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(User user)
{
    if (!ModelState.IsValid)
    {
        var viewModel = new RegisterFormViewModel
        {
            Users = user,
            Teams = _context.Team.ToList()
        };

        return View("Register", viewModel);
    }

    if (user.Id == 0)
    {
        _context.User.Add(user);
    }
    else
    {
        var usersInDb = _context.User.Single(m => m.Id == user.Id);
        usersInDb.FirstName = user.FirstName;
        usersInDb.LastName = user.LastName;
        usersInDb.TeamCategory = user.TeamCategory;
        usersInDb.RefIndicator = user.RefIndicator;
        usersInDb.UserRoles = user.UserRoles;
        usersInDb.IsRegistered = user.IsRegistered;
        usersInDb.Email = user.Email;
        usersInDb.DateRegistered = user.DateRegistered;
    }

    _context.SaveChanges();

    return RedirectToAction("Index", "User");
}

保存操作基本上做了两件事......它保存了一个新表单,它也用于更新数据。

如果我的代码中的错误可以被删除,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

根据我的理解,您需要将ID设为自动递增值。

awk -i inplace ...

我认为它不适用于public class User { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string RefIndicator { get; set; } public Team TeamCategory { get; set; } public byte TeamId { get; set; } public bool IsRegistered { get; set; } public DateTime DateRegistered { get; set; } public DateTime? LastModified { get; set; } public UserRoles UserRoles { get; set; } public byte UserRolesId { get; set; } } ,但您可以肯定使用byte

int