I have table in SQL Server management studio and then going to Visual Studio -> Server Explorer -> Add connection, my table is added successfully.
Problem is data is not inserted into SQL Server when I press a create button then not any error shown, I think the record is created but when I refresh in SQL Server Management Studio, in the table, the record is not displayed...
- 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; }
- }
Context class is the bridge between the database and application:
- using System.Data.Entity;
-
- public class studcontext:DbContext
- {
- public DbSet 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();
- }
- }
InsertData.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>
I already created a database in SQL Server Management Studio, and adding a database in my project successfully. But when I click on the create button, the code does not generate any errors, but after a refresh of my database, the record is not shown....
I put the viewbag.message="record successfully" the message did not display and record not display in SQL Server?
Can someone please help? What am I doing wrong?