【问题标题】:How to add auto generated ID continued from my last Data?如何从我的最后一个数据继续添加自动生成的 ID?
【发布时间】:2014-08-05 07:43:37
【问题描述】:

我想自动生成从我以前的 ID 递增的 ID。 ID 格式为 A00001, A00002,.... 我不知道如何自动生成

控制器

[HttpPost]
    public ActionResult Create(Assignment assignment)
    {

        if (ModelState.IsValid)
        {

            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase assignmentFile = Request.Files[0];
                if (assignmentFile.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(assignmentFile.FileName);
                    assignment.FileLocation = Path.Combine(Server.MapPath("~/Content/File"), fileName);
                    assignmentFile.SaveAs(assignment.FileLocation);
                }


            }
            db.Assignments.Add(assignment);

            db.SaveChanges();

            return RedirectToAction("Index");
        }
        return View(assignment);
    }

型号

  public partial class Assignment
   {
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }

    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation { get; set; }

    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}

查看

 <% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
 { %>
  <%: Html.ValidationSummary(true) %>

<fieldset>
    <legend>Assignment</legend>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.SubmissionDate) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.SubmissionDate) %>
        <%: Html.ValidationMessageFor(model => model.SubmissionDate) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Status) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Status) %>
        <%: Html.ValidationMessageFor(model => model.Status) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Mark) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Mark) %>
        <%: Html.ValidationMessageFor(model => model.Mark) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Comments) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Comments) %>
        <%: Html.ValidationMessageFor(model => model.Comments) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.FileLocation) %>
    </div>
    <div class="editor-field">
       <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

我想自动生成从我以前的 ID 递增的 ID。 ID 格式为 A00001, A00002,.... 我不知道如何自动生成

【问题讨论】:

  • 你从哪里得到数据库中最后一个作业的 ID?
  • 我不知道如何使用 C#/MVC4 获取最后一个 ID
  • 返回所有作业集合的方法是什么?例如IEnumerable&lt;Assignment&gt; assignments = db.Assignments
  • 呃,我还没有得到最后一个 ID,因为我不知道在哪里可以找到它

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


【解决方案1】:

试试这个

        db.Assignments.Add(assignment);
        db.SaveChanges();
        Var AssignmentID = assignment.Id;

【讨论】:

    【解决方案2】:

    一开始我误解了你的问题。

    你需要身份是第一个字母A的字符串吗?如果您出于视觉目的需要它,您可以只使用数据库的标识属性来自动递增并使用 get 来构建字符串吗?

    以下代码用于代码优先迁移。如果您使用的是数据库优先方法,则不需要 [Key] 和 [NotMapped] 注释。在这种情况下,只需使用身份(1,1)创建 Id 属性。

    public partial class Assignment
    {
        public Assignment()
        {
            this.CourseAvailables = new HashSet<CourseAvailable>();
        }
    
        [Key]
        private int _id; // Auto incremented id
    
        [NotMapped]
        public string AssignmentID
        {
            get
            {
                return "A" + _id.ToString("D5");
            }
        }
        public DateTime? SubmissionDate { get; set; }
        public string Status { get; set; }
        public decimal? Mark { get; set; }
        public string Comments { get; set; }
        public string FileLocation { get; set; }
    
        public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
    }
    

    这样您将获得数据库自动增量的可靠性以及在类定义中格式化特殊 ID 的灵活性。

    编辑:彻底修改了我的答案

    【讨论】:

    • 发生错误,关系“Model.FK__CourseAva__Assig__4222D4EF”未加载,因为类型“Model.Assignment”不可用。,
    【解决方案3】:

    试试这个,伙计,

    con.Open();
                string sqlQuery = "SELECT TOP 1 kode_user from USERADM order by kode_user desc";
                SqlCommand cmd = new SqlCommand(sqlQuery, con);
                SqlDataReader dr = cmd.ExecuteReader();
    
                while (dr.Read())
                {
                    string input = dr["kode_user"].ToString();
                    string angka = input.Substring(input.Length - Math.Min(3, input.Length));
                    int number = Convert.ToInt32(angka);
                    number += 1;
                    string str = number.ToString("D3");
    
                    txtKodeUser.Text = "USR" + str;
                }
                con.Close();
    

    代码将获取数据库中的最后一个 id,然后仅从字符串中获取数字并将数字加一。 这是代码从字符串中获取数字的行:

    string angka = input.Substring(input.Length - Math.Min(3, input.Length));
    

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多