Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I am struggling to save fields to my DB, at first i was using one model to save them and it was(TbRegistrationForm). Now my form uses multiple models in one View and my submit its not saving these fields to the table definition.

What I have tried:

C#
<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);
}


// Models
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; }

       
    }


// Data-Access-Layer
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);
        }
    }
 }


// View
@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>
Posted
Updated 9-Jul-20 2:15am

1 solution

The answer depends on your table structure, but either have a model that comprises data for both tables that you use in your view, then programatically extract the properties from that single model and create the two individual models to save to your database, or if your tables are related you can map two tables to a single entity and use that entity as your view model.

Designer Entity Splitting - EF6 | Microsoft Docs[^]

An alternative to creating a single big model with all properties would be to create a model that has a property for both database models.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900