【发布时间】: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<Assignment> assignments = db.Assignments -
呃,我还没有得到最后一个 ID,因为我不知道在哪里可以找到它
标签: c# asp.net-mvc-4 auto-generate