asp.net-mvc


Edit page with a foreign key constraint fails in Asp.net MVC


Trying to get familiar with EF model, but I am having trouble at this point - foreign key problem.
So I have two models.
public class Employee
{
[Key]
public int EmpId { get; set; }
public string Name { get; set; }
public int WorkingDateTimeId { get; set; }
public virtual WorkingDateTime WorkingDateTimes { get; set; }
}
public class WorkingDateTime
{
[Key]
public int WorkingDateTimeId { get; set; }
public string Day { get; set; }
}
Creating Employee information works fine. So in my create view, I enter Employee Name, and WorkingDateTime information, which surprised me it automatically creates a row in WorkingDateTime table.
<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.WorkingDateTimes.Day, "Day: ", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.WorkingDateTimes.Day, new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
However, when I try to edit the specific Employee row, I get this error:
Cannot add or update a child row: a foreign key constraint fails ("test"."employees", CONSTRAINT "FK_Employees_WorkingDateTimes_WorkingDateTimeId" FOREIGN KEY ("WorkingDateTimeId") REFERENCES "workingdatetimes" ("WorkingDateTimeId") ON DELETE CASCADE O)
I am binding these properties in my controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "EmpId,Name,WorkingDateTimes")] Employee employee)
What do I need in order to Edit successfully?

Related Links

MVC equivalent of jQuery.load
mvc 4 I've deployed to production server. Now cannot connect to localdb
Extra Paging Section at the Top of Kendo UI Grid using Razor
MVC 5- Application showing error message for System.Web.WebPages.Html.HtmlHelper for controls like viewbag
How to get custom date [only YYYY] validated MVC razor?
MVC Survey and capturing data
ReadOnly Form Properties
Add string prefix to automapped model property
MVC 4.0 Clearing output cache using HttpResponse.RemoveOutputCacheItem
How to disable open ASP.NET MVC site in IFrame?
Index ActionMethod in Home Controller would be called twice in ASP.Net MVC
how to determine if page is using http or https in a view?
Azure MVC4 RequireHttps not redirecting to https site
How can I store the Web Application Username in a DB UpdatedBy or CreatedBy column?
Expand IQueryable to use custom linq extension
MVC Controller to get image with ChildActionOnly

Categories

HOME
regex
performance
opencl
direction
blogs
path
datetime
swashbuckle
wikipedia
api.ai
active-directory
delphi-xe7
webdriver-io
tcplistener
uwsgi
signature
cloudrail
smartsheet-c#-sdk-v2
lag
partitioning
org-mode
hp-alm
google-finance
slime
icloud-drive
shinydashboard
dapper-fastcrud
akka-http
magento2.0.2
evercookie
bcel
spring-data-cassandra
gradient-descent
jdom-2
xaf
confirm
decompiler
sagepay
psychopy
wso2iot
gradle-tooling-api
backwards-compatibility
couchbase-lite
gretl
gulp-babel
ewsjavaapi
sql-mode
xbrl
view-source
jawr
manifoldcf
agile-processes
sqlite-net
amazon-dynamodb-streams
webtrends
codeigniter-upload
webclient-download
mutation
highslide
google-api-ruby-client
dual-table
openocd
json4s
core-data-migration
dhc
structured-programming
gcovr
information-schema
jasmin
datacontractserializer
dojo.gridx
custom-titlebar
windward
jxta
location-client
buildout
broadcom
file-structure
guvnor
ruby-1.9.2
b-method
azure-role-environment
retain
paredit
myisam
seam-conversation
high-traffic
executescalar
templatebinding
virtual-server
variable-names

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App