I have a problem and I don't know why am I getting this bug. - CREATE TABLE Course(
-
IdCourse int IDENTITY (1,1) NOT NULL PRIMARY KEY,
-
NameCourse nvarchar(50) NOT NULL,
-
Description nvarchar(250) NOT NULL,
-
Date datetime NOT NULL,
-
Full BIT NULL DEFAULT 0
-
)
-
-
CREATE TABLE Application
-
(
-
IdApplication int IDENTITY (1,1) NOT NULL PRIMARY KEY,
-
Date datetime NOT NULL,
-
Name nvarchar(25) NOT NULL,
-
Surname nvarchar(25) NOT NULL,
-
Adress nvarchar(50) NOT NULL,
-
Email nvarchar(320) NOT NULL,
-
Telephone varchar(15) NOT NULL,
-
IdCourse int FOREIGN KEY REFERENCES Course(IdCourse),
-
Status INT not nulL
-
)
-
-
-
public ActionResult Application(int id)
-
{
-
ViewBag.Course = (
-
from c in db.Course
-
where c.IdCourse == id
-
select s.NameCourse).FirstOrDefault();
-
Application application = new Application();
-
return View(application);
-
}
-
-
[HttpPost]
-
[ValidateAntiForgeryToken]
-
public ActionResult Application([Bind(Include = "IdApplication,Name,Surname,Adress,Email,Telephone")] Application application)
-
{
-
Course course = new course();
-
application.IdCourse = course.IdCourse;
-
int applicationId = application.IdApplication;
-
-
if (ModelState.IsValid)
-
{
-
db.Applications.Add(Application);
-
db.SaveChanges();
-
return RedirectToAction("Index");
-
}
-
-
return View(application);
-
}
-
My view -
@model Aplikacija.Models.Applicaton
-
-
@{
-
<strong>@ViewBag.Course</strong>
-
}
-
-
-
@using (Html.BeginForm())
-
{
-
@Html.AntiForgeryToken()
-
-
<div class="form-horizontal"> <hr />
-
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
-
@Html.HiddenFor(model => model.IdApplication)
-
-
<div class="form-group">
-
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group">
-
@Html.LabelFor(model => model.Telephone, htmlAttributes: new { @class = "control-label col-md-2" })
-
<div class="col-md-10">
-
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
-
@Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
-
</div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-primary" /> </div> </div> </div>
-
}
-
-
<div>
-
@Html.ActionLink("Back to List", "Index")
-
</div>
-
-
@section Scripts {
-
@Scripts.Render("~/bundles/jqueryval")
-
}
2 2329 zmbd 5,501
Expert Mod 4TB
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint
Is that the full error message?
Typically, this would indicate that you have a value in the column for the foreign key reference to another table that is not also in the primary key column of the referenced table.
Without the full error message that's about as far as we can offer...
I changed my HttpPost and there is no exception anymore. It just gives me new id of the Course table and I don't want that. I need to place it in the row that I chose in ViewBag from GET. - [HttpPost]
-
[ValidateAntiForgeryToken]
-
public ActionResult Application(Application application)
-
{
-
application.Course = new Course();
-
if (ModelState.IsValid)
-
{
-
db.Applications.Add(application);
-
db.SaveChanges();
-
return RedirectToAction("Index");
-
}
-
-
ViewBag.Course = new SelectList(db.Courses, "IdCourse", "NameCourse", application.IdCourse);
-
return View(application);
-
}
Thank you for your reply.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J. Muenchbourg |
last post by:
The block of code below shows how I am inserting field values into my
dbase table:
strSQLStatement = "INSERT INTO tblArticles
(handid,ArticleDate,sport,articleheader, fpick,articleText) "_
&...
|
by: Tim::.. |
last post by:
Can someone please tell me why I keep getting the following error from the
code below!
Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
|
by: Geoffrey KRETZ |
last post by:
Hello,
I'm wondering if the following behaviour is the correct one for
PostGreSQL (7.4 on UNIX).
I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching
the following request :...
|
by: macupryk |
last post by:
{System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ProjectResponse__ProjectQuestionId". The conflict occurred in database "RG_ProjectData", table...
|
by: filip1150 |
last post by:
I'm trying to find if there is any performance diference between
explicitly using a sequence in the insert statement to generate values
for a column and doing this in an insert trigger.
I...
|
by: weird0 |
last post by:
I have two tables accounts and ATM and i am trying to insert a tuple
in ATM with accountId as foreign key. But even this simple work,I
encounter the following error:
The INSERT statement...
|
by: haiminnu |
last post by:
I have created Two tables
1]EmployeeTable
--------------------------
EmpID
EmpName
AccessLevelID ...
|
by: ksenthilbabu |
last post by:
Hey All,
I am using MSSQL -2005 with VB6.
I have created a master table tblCompany and detail Table tblDetail having foreign key relationship.
When i try to insert a value within a TRANSACTION I...
|
by: bougie |
last post by:
i have two tables projects and proposals
in project I have column ID and in proposal I have column ID too and there is a one to one realtionship between these tables the primary key in this realtion...
|
by: Good Guy |
last post by:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Customers_Contact".
The conflict occurred in database "BreakAway", table "dbo.Contact", column 'ContactID'.
The statement has...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |