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) –