我在SQL Server Management Studio中有表,然后转到Visual Studio->服务器资源管理器->添加连接,我的表已成功添加。
问题是当我按下创建按钮时未在SQL Server Management Studio中插入数据,但未显示任何错误,我认为该记录已创建,但是当我在SQL Server Management Studio中刷新表记录时未显示?
table:empls
empid int primary key
empname varchar(50)
empsalary varchar(50)
empage int
class:
[Table("empls")]
public class stdimg
{
[Key]
public int empid { get; set; }
public string empname { get; set; }
public string empsalary { get; set; }
public int empage { get; set; }
}
contextclass:context类是数据库和应用程序之间的桥梁
使用System.Data.Entity;
public class studcontext:DbContext
{
public DbSet<stdimg> stds { get; set; }
}
HomeController:
public class HomeController : Controller
{
studcontext _context = new studcontext();
[HttpGet]
public ActionResult InsertData()
{
return View();
}
[HttpPost]
public ActionResult InsertData(stdimg studs)
{
_context.stds.Add(studs);
_context.SaveChanges();
ViewBag.message = "data inserted successfully";
return View();
}
}
InserData.cshtml
@model ImageUploadUsingMvc.Models.stdimg
@{
ViewBag.Title = "InsertData";
}
<h2>InsertData</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>stdimg</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.empname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.empname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.empname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.empsalary, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.empsalary, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.empsalary, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.empage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.empage, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.empage, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
我已经在SQL Server Management Studio中创建了一个数据库,并成功地在项目中添加了数据库。但是当我按下create按钮时,不会产生任何错误并刷新我的数据库,然后记录不显示?我将viewbag.message =“录制成功”消息不显示,并且记录未显示在ssms中?
您未在Html.BeginForm()上执行发布然后,我建议您为该工作创建独立的服务