TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
2.1k
469.4k
How to save changes to db, using mutliple models in one view?
Jul 9 2020 6:43 AM
Hi team
I have multiple models in my View, the issue my form does not save to my db. I have before when used one it was saving well all changes to my db. Kindly please help me to improve my logic better and stuck.
// Controller
//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);
}
// 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);
}
}
}
// 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
; }
}
// View
@
using
eNtsaRegistrationTraining.Models
@model RegViewAndRoleViewModel
@{
ViewBag.Title =
"SubmitRegDetails"
;
}
Its not sending to db, but if i am using one model(TbRegistrationForm only) it does save straight to my DB with all form fields.
Reply
Answers (
6
)
How i expire user Session if a user opens a new tab or copies a URL in
Trying to build an MVC project for people to book a spa appointment