NULL我有一个简单mvc5
码第一应用,它具有在后端和MS SQL数据库,并在前端的形式。插入成功,但插入的值示出了在后端MS SQL数据库
虽然我通过前端表单插入数据库,但它不会产生任何错误,一切似乎都正常,但是当我检查后端数据库表时,则新插入的行中的所有值显示为NULL
。
这是我的模型代码:
public class students
{
public int Id { get; set; }
[Display(Name = "Name")]
public string st_name { get; set; }
[Display(Name = "Father's Name")]
public string st_father_name { get; set; }
public string st_contact { get; set; }
}
这是视图模型类:
public class AddStudentViewModel
{
public students stdntss { get; set; }
}
这是控制器:
public ActionResult Index()
{
var std = _context.stdnts;
if (std==null)
{
return Content("Nothing Found");
}
return View(std);
}
public ActionResult AddStudent()
{
return View();
}
[HttpPost]
public ActionResult Insert(students st)
{
_context.stdnts.Add(st);
_context.SaveChanges();
return RedirectToAction("Index","Students");
}
最后是这样的观点:
@model school2.ViewModels.AddStudentViewModel
@{
ViewBag.Title = "AddStudent";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>New student's registration form</h2>
@using (Html.BeginForm("Insert","Students"))
{
<div class="form-group">
@Html.LabelFor(m=> m.stdntss.st_name)
@Html.TextBoxFor(m=> m.stdntss.st_name, new { @class="form-control"})
</div>
<div class="form-group">
@Html.LabelFor(m => m.stdntss.st_father_name)
@Html.TextBoxFor(m => m.stdntss.st_father_name, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.stdntss.st_contact)
@Html.TextBoxFor(m => m.stdntss.st_contact, new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-primary">Save</button>
}
如果有人有任何线索,请帮助我吗?解决这个
第一件事:提交表单后'st'具有什么功能? –
[类的命名属性指南是Pascal公约](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members)。显然,这只是一个指导方针,但是为什么性能降低套管其他开发商可能会混淆(例如'st_father_name',而不是''StFatherName) –