从MVC表单将数据发布到Visual Studio本地数据库

时间:2019-06-11 13:40:43

标签: c# asp.net model-view-controller

我正在尝试将数据从MVC注册表单保存到Visual Studio本地数据库中,但是我没有将数据保存到数据库中。任何帮助将不胜感激。

在我的另一个项目中,数据已正确存储在本地数据库表中

HomeController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OQT_P1._2.Models;

namespace OQT_P1._2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }
        [HttpPost]
        [ActionName("Contact")]
        public ActionResult Contact_Post()
        {
            Contact contact = new Contact();
            TryUpdateModel(contact);

            if (ModelState.IsValid)
            {
                AddComment(contact);
                return RedirectToAction("Registration");
            }
            return View();
        }
        public ActionResult AddComment(Contact c)
        {
            OQContext db = new OQContext();
            db.Contacts.Add(c);
            db.SaveChanges();
            return View();
        }
        [HttpGet]
        [ActionName("Registration")]
        public ActionResult Registration_Get()
        {

            return View();
        }
        [HttpPost]
        [ActionName("Registration")]
        public ActionResult Registration_Post()
        {
            Registration register = new Registration();
            TryUpdateModel(register);

            if (ModelState.IsValid)
            {
                AddStudent(register);

                return RedirectToAction("Registration");
            }


            return View();
        }
        public ActionResult AddStudent(Registration r)
        {
            OQContext db = new OQContext();
            db.Registrations.Add(r);
            db.SaveChanges();
            return View();
        }

}
}

注册视图:

@model OQT_P1._2.Models.Registration

@{
    ViewBag.Title = "Registration";
    Layout = "~/Views/Shared/_adminLayout.cshtml";
}

<style>
    .myactionlink:hover {
        color: white;
        text-decoration-line: none;
    }

    .myactionlink {
        color: gray;
    }
</style>


<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="container regform">

            <form action="/action_page.php" class="regform1">
                <h3 style="text-align: center;">Register Here!</h3>
                <p>Please fill up the registration form given below and click on submit.</p>
                <div class="container-fluid regform1_1" style="float: left;">
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="email">Student Id:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.StudentId, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.StudentId, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="email">Student Name:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Student Name" } })
                            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">Guardian/Parent Name:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.Parent, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Parent, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="email">Email Address:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">Landline No:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.landline, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.landline, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">Mobile No:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">Skype ID:</label></div>
                        <div class="col-md-8 regf">
                            @Html.EditorFor(model => model.SkypeID, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.SkypeID, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">Country:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.DropDownList("Country", new List<SelectListItem> { new SelectListItem { Text = "Male", Value = "Male" },
                      new SelectListItem { Text = "Canada", Value = "Canada" },
                      new SelectListItem { Text = "China", Value = "China" },
                      new SelectListItem { Text = "Cyprus", Value = "Cyprus" },
                      new SelectListItem { Text = "Denmark", Value = "Denmark" },
                      new SelectListItem { Text = "Finland", Value = "Finland" },
                      new SelectListItem { Text = "France", Value = "France" },
                      new SelectListItem { Text = "Germany", Value = "Germany" },
                      new SelectListItem { Text = "Hong Kong S.A.R., China", Value = "Hong Kong S.A.R., China" },
                      new SelectListItem { Text = "Hungary", Value = "Hungary" },
                      new SelectListItem { Text = "Indonesia", Value = "Indonesia" },
                      new SelectListItem { Text = "Ireland", Value = "Ireland" },
                      new SelectListItem { Text = "Italy", Value = "Italy" },
                      new SelectListItem { Text = "Japan", Value = "Japan" },
                      new SelectListItem { Text = "Malaysia", Value = "Malaysia" },
                      new SelectListItem { Text = "Netherlands", Value = "Netherlands" },
                      new SelectListItem { Text = "New Zealand", Value = "New Zealand" },
                      new SelectListItem { Text = "Norway", Value = "Norway" },
                      new SelectListItem { Text = "Russia", Value = "Russia" },
                      new SelectListItem { Text = "Singapore", Value = "Singapore" },
                      new SelectListItem { Text = "South Africa", Value = "South Africa" },
                      new SelectListItem { Text = "South Korea", Value = "South Korea" },
                      new SelectListItem { Text = "Spain", Value = "Spain" },

                      new SelectListItem { Text = "Afghanistan", Value = "Afghanistan" },
                      new SelectListItem { Text = "Aland Islands", Value = "Aland Islands" },
                      new SelectListItem { Text = "Albania", Value = "Albania" },
                      new SelectListItem { Text = "Algeria", Value = "Algeria" },
                      new SelectListItem { Text = "American Samoa", Value = "American Samoa" },
                      new SelectListItem { Text = "Andorra", Value = "Andorra" },
                      new SelectListItem { Text = "Angola", Value = "Angola" },
                      new SelectListItem { Text = "Anguilla", Value = "Anguilla" },
                      new SelectListItem { Text = "Antarctica", Value = "Antarctica" },

                       "Select Country", new { style = "width: 100%;" })
                            @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
                        </div>

                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">State/Provence:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.provence, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.provence, "", new { @class = "text-danger" })
                        </div>

                    </div>
                    <div class="row no-gutters">
                        <div class="col-sm-4 regf"> <label for="text">City:</label></div>
                        <div class="col-sm-8 regf">
                            @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
                        </div>

                    </div>

                    <div class="row no-gutters">

                        <div class="col-md-4 regf"> <label for="pwd">Best contact time:</label></div>
                        <div class="col-md-8 regf">

                            @using (Html.BeginForm("Registration", "Home", FormMethod.Post))
                            {

                                <span>Morning:</span>  @Html.CheckBoxFor(m => m.Morning, true)<br />
                                <span> Day:</span>  @Html.CheckBoxFor(m => m.Day, false)<br />
                                <span> Evening:</span>  @Html.CheckBoxFor(m => m.Evening, false)
                            }
                            @*<div class="checkbox">


                                    <label style="padding: 0 0 0 20px"><input type="checkbox" id="morning" name="gtime"> Morning</label>
                                    <label style="padding: 0 0 0 20px"><input type="checkbox" id="day" name="gtime"> Day</label>
                                    <label style="padding: 0 0 0 20px"><input type="checkbox" id="evening" name="gtime"> Evening</label>
                                </div>*@
                        </div>
                        <!--    style="border:1px solid gray;"<div class="col-md-8 regf" >
                                       <input type="checkbox" name="remember">
                        <label class="form-check-label"> <div class="form-check">

                             <input class="form-check-input" type="checkbox" name="remember" required >
                             <div class="valid-feedback">Valid.</div>
                             <div class="invalid-feedback">Check this checkbox to continue.</div>  </div> </label>

                                   </div>-->
                    </div>
                    <div class="row no-gutters">

                        <div class="col-md-4 regf"> <label for="pwd">Message:</label></div>
                        <div class="col-md-8 regf">
                            @*<textarea placeholder="Type your Message here" class="form-control form-textarea" id="comments" name="comments" cols="60" rows="5" style="opacity: 1; height: 193px;"></textarea>*@
                            @Html.TextArea("Message", null, new { @class = "form-control", @placeholder = "Type your Message here" })

                        </div>
                    </div>


                </div>
                <div class="row no-gutters">

                    <div class="col-md-4 regf"> <label for="pwd"></label></div>
                    <div class="col-md-8 regf" style="margin-bottom: 20px;">
                        @*<button type="submit" class="btn btn-primary" style="width: 200px; margin-left: 5px;">Submit Registration Form</button>*@
                        <input type="submit" class="btn btn-primary" style="width: 240px; margin-left: 5px;" value="Submit Registration Form" />
                    </div>
                </div>

            </form>
        </div>
    }
    <hr>

</body>
</html>

注册模型类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace OQT_P1._2.Models
{
    [Table("StudentTable")]
    public class Registration
    {
        [Key]
        public int StudentId { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Parent { get; set; }
        [EmailAddress]
        public string Email { get; set; }
        public string landline { get; set; }
        [Required]
        public string Mobile { get; set; }

        public string Country { get; set; }
        [Required]
        public string provence { get; set; }
        public string City { get; set; }

        public bool BCT { get; set; }
        [Required]
        public string SkypeID { get; set; }
        [StringLength(400, MinimumLength = 20)]
        public string Message { get; set; }

        public bool Morning { get; set; }
        public bool Day { get; set; }
        public bool Evening { get; set; }
    }
}

上下文类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace OQT_P1._2.Models
{
    public class OQContext : DbContext
    {
        public DbSet<Registration> Registrations { get; set; }
        public DbSet<Contact> Contacts { get; set; }

        public System.Data.Entity.DbSet<OQT_P1._2.Models.Login> Logins { get; set; }

        public System.Data.Entity.DbSet<OQT_P1._2.Models.Forgotpassword> Forgotpasswords { get; set; }

        public System.Data.Entity.DbSet<OQT_P1._2.Models.PasswordReset> PasswordResets { get; set; }
    }
}

表定义:

CREATE TABLE [dbo].[StudentTable] (
    [Id]         INT            NOT NULL,
    [Name]       NVARCHAR (50)  NOT NULL,
    [ParentName] NCHAR (10)     NULL,
    [Email]      NVARCHAR (50)  NOT NULL,
    [LandlineNo] NVARCHAR (50)  NULL,
    [MobileNo]   NVARCHAR (50)  NULL,
    [SkypeId]    NVARCHAR (50)  NOT NULL,
    [Country]    NVARCHAR (50)  NULL,
    [State]      NVARCHAR (50)  NULL,
    [City]       NVARCHAR (50)  NULL,
    [Morning]    BIT            NULL,
    [Day]        BIT            NULL,
    [Evening]    BIT            NULL,
    [Message]    NVARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

我提交表单时没有错误,但是表单数据未存储在数据库表中。

0 个答案:

没有答案