Как сохранить изменения в БД с помощью нескольких моделей в одном представлении в ASP.NET MVC?
Привет Команда
Я изо всех сил пытаюсь сохранить поля в своей БД, сначала я использовал одну модель для их сохранения, и это была(TbRegistrationForm). Теперь моя форма использует несколько моделей в одном представлении, и моя отправка не сохраняет эти поля в определении таблицы.
Что я уже пробовал:
<pre>// Controller <pre> public ActionResult SaveRegForm() { ViewBag.Message = "Details saved successfull"; return View(db.TrainingRegs.ToList()); }
//GET:TrainingRegForm/Create/WebRequest. [HttpPost] [ValidateAntiForgeryToken] public ActionResult SubmitRegDetails([Bind(Include= "Id, Title, FirstName, LastName, Position, Company, StreetAddress, StreetAddressLine, City, StateProvince, ZipCode,Country,Email, CellNumber, DietaryRequirement")]TrainingRegForm eNtsaTraining) { this.ViewBag.SubmitRegDetails = this.SaveRegForm(); if(ModelState.IsValid) { eNtsaTraining.Id = Guid.NewGuid(); db.TrainingRegs.Add(eNtsaTraining); db.SaveChanges(); return RedirectToAction("SaveRegForm"); } // Validates when empty. if(ModelState.IsValid) { return RedirectToAction("SaveRegForm"); } return View(eNtsaTraining); }
// Модельный
namespace eNtsaRegistrationTraining.Models { public class TrainingRegForm { [Key] public Guid? Id { get; set; } public string Title { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Position { get; set; } public string Company { get; set; } public string StreetAddress { get; set; } public string StreetAddressLine { get; set; } public string City { get; set; } public string StateProvince { get; set; } public int ZipCode { get; set; } public string Country { get; set; } public string Email { get; set; } [Required(ErrorMessage = "This field is required")] [DataType(DataType.PhoneNumber)] public string CellNumber { get; set; } public string DietaryRequirement { get; set; } } public class RegViewAndRoleViewModel { public DietViewModel DietMain { get; set; } public TrainingRegForm RegForm { get; set; } public DropDownViewModel ListCountries { get; set; } public RegistrationTrainingForm HomeModel { get; set; } public RoleViewModel RoleViewModelData { get; set; } }
// Уровень Доступа К Данным
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using eNtsaRegistrationTraining.Models; namespace eNtsaRegistrationTraining.DAL { public class eNtsaRegistration:DbContext { public eNtsaRegistration() : base("eNtsaRegistration") { } public DbSet<TrainingRegForm> TrainingRegs { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } }
// Вид
@using (Html.BeginForm("SubmitRegDetails", "Home", FormMethod.Post)) { <div class="form-horizontal"> <hr /> <div class="form-group row"> <label for="Title" class="col-sm-2 col-form-label">Title</label> <div class="col-sm-3 "> @Html.EditorFor(model => model.RegForm.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.RegForm.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="Name" class="col-sm-2 col-form-label">Name:</label> <div class="col-sm-3 "> @Html.EditorFor(model => model.RegForm.FirstName, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Name" } }) @Html.ValidationMessageFor(model => model.RegForm.FirstName, "", new { @class = "text-danger" }) </div> <div class="col-sm-3"> @Html.EditorFor(model => model.RegForm.LastName, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "LastName" } }) @Html.ValidationMessageFor(model => model.RegForm.LastName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="Position" class="col-sm-2 col-form-label">Position:</label> <div class="col-sm-3"> @Html.EditorFor(model => model.RegForm.Position, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.RegForm.Position, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="Company" class="col-sm-2 col-form-label">Company:</label> <div class="col-md-3"> @Html.EditorFor(model => model.RegForm.Company, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.RegForm.Company, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="Addresss" class="col-sm-2 col-form-label">Address*</label> <div class="col-sm-5"> @Html.EditorFor(model => model.RegForm.StreetAddress, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Street Address" } }) @Html.ValidationMessageFor(model => model.RegForm.StreetAddress, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="Address" class="col-sm-2 col-form-label"></label> <div class="col-sm-5"> @Html.EditorFor(model => model.RegForm.StreetAddress, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Street Address Line 2" } }) @Html.ValidationMessageFor(model => model.RegForm.StreetAddress, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="City" class="col-sm-2 col-form-label"></label> <div class="col-sm-3"> @Html.EditorFor(model => model.RegForm.City, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "City" } }) @Html.ValidationMessageFor(model => model.RegForm.City, "", new { @class = "text-danger" }) </div> <label for="State/Province" class="col-form-label"></label> <div class="col-sm-3"> @Html.EditorFor(model => model.RegForm.StateProvince, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "State/Province" } }) @Html.ValidationMessageFor(model => model.RegForm.StateProvince, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="ZipCode" class="col-sm-2 col-form-label"></label> <div class="col-sm-3"> @Html.EditorFor(model => model.RegForm.ZipCode, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "ZipCode" } }) @Html.ValidationMessageFor(model => model.RegForm.ZipCode, "", new { @class = "text-danger" }) </div> <div class="col-sm-3"> @Html.DropDownListFor(m=>m.ListCountries.Country, ViewBag.CountryList as SelectList) </div> </div> <div class="form-group row"> <label for="Email" class="col-sm-2 col-form-label">Email:</label> <div class="col-sm-4"> @Html.EditorFor(model => model.RegForm.Email, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Email" } }) @Html.ValidationMessageFor(model => model.RegForm.Email, "", new { @class = "text-danger" }) </div> </div> <div class="form-group row"> <label for="Attendee" class="col-sm-2 col-form-label">Attendee Cell Number*</label> <div class="col-sm-3"> @Html.EditorFor(model => model.RegForm.CellNumber, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Cell Number" } }) @Html.ValidationMessageFor(model => model.RegForm.CellNumber) </div> </div>