Click here to Skip to main content
15,890,741 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionGenerate SQL query Pin
Member 128253814-Nov-16 6:23
Member 128253814-Nov-16 6:23 
AnswerRe: Generate SQL query Pin
Richard MacCutchan4-Nov-16 6:58
mveRichard MacCutchan4-Nov-16 6:58 
QuestionOperation must use an updateable query - question Pin
Member 87616672-Nov-16 15:59
Member 87616672-Nov-16 15:59 
AnswerRe: Operation must use an updateable query - question Pin
Richard Deeming3-Nov-16 3:10
mveRichard Deeming3-Nov-16 3:10 
QuestionCrud operation by pattern Pin
Member 1282618731-Oct-16 20:27
Member 1282618731-Oct-16 20:27 
AnswerRe: Crud operation by pattern Pin
Nathan Minier1-Nov-16 2:12
professionalNathan Minier1-Nov-16 2:12 
AnswerRe: Crud operation by pattern Pin
F-ES Sitecore1-Nov-16 5:46
professionalF-ES Sitecore1-Nov-16 5:46 
QuestionSave Foreign Key Value - EF Core + ASP.NET Core Pin
vanilsonwdd31-Oct-16 19:43
vanilsonwdd31-Oct-16 19:43 
Hi guys, I´m new to ASP.NET Core and EF Core and I have the following problem:

I have two related objects, Goals and Months, Goals got the foreign key of Months, and when I try to save the data of goals I got a foreign key error because the value of MonthId is 0, but when I create the select in the HTML I got correct code value.

Description of the error:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint FK_Goals_MonthId;. The conflict occurred in database Portal_RNT, table dbo.Months , column MonthId;.
The statement has been terminated.


Here is the code:

HTML

HTML
@model ProjectName.Models.Goals
<div>
   <form asp-controller="Goals" asp-action="Create">
      Other form elements

      <select name="months" asp-for="MonthId" class="form-control" asp-items="ViewBag.Months">

      </select>

      <input type="submit" value="Save" class="btn btn-primary" />
   </form>
</div>


Goals Model

C#
public class Goals
    {
        [Display(Name = "Code")]
        public Int32 GoalId { get; set; }

        public String GoalName{ get; set; }
        public Months Month{ get; set; }
        public Int32 MonthId{ get; set; }
        ....
        Other properties

    }


Month Model

C#
public class Months
{
    [Key]
    [Display(Name = "Code")]
    public Int32 MonthId { get; set; }
    [Display(Name = "Designation")]
    public String Designation{ get; set; }
}


Goals Controller

C#
public IActionResult Create(Goals goal)
{
   if (ModelState.IsValid)
   {
      _context.Goals.Add(goal);
      _context.SaveChanges();
   }
   return View("~/../Create.cshtml");
}


Best Regards
QuestionOptimal way to share data between MVC Controller and AngularJS Pin
L_Q31-Oct-16 10:50
L_Q31-Oct-16 10:50 
AnswerRe: Optimal way to share data between MVC Controller and AngularJS Pin
Nathan Minier1-Nov-16 2:25
professionalNathan Minier1-Nov-16 2:25 
GeneralRe: Optimal way to share data between MVC Controller and AngularJS Pin
L_Q1-Nov-16 2:42
L_Q1-Nov-16 2:42 
GeneralRe: Optimal way to share data between MVC Controller and AngularJS Pin
Nathan Minier1-Nov-16 3:11
professionalNathan Minier1-Nov-16 3:11 
QuestionMultiple Date Rages in Multi select Drop down list in HTML 5 using JQuery or AngularJS Pin
indian14331-Oct-16 6:25
indian14331-Oct-16 6:25 
QuestionFull crud operation in ASP.NET in a single page with dropdownlist, radio button and checkbox controls Pin
Member 1273685631-Oct-16 1:26
Member 1273685631-Oct-16 1:26 
GeneralRe: Full crud operation in ASP.NET in a single page with dropdownlist, radio button and checkbox controls Pin
Richard MacCutchan31-Oct-16 1:41
mveRichard MacCutchan31-Oct-16 1:41 
AnswerRe: Full crud operation in ASP.NET in a single page with dropdownlist, radio button and checkbox controls Pin
ZurdoDev31-Oct-16 1:54
professionalZurdoDev31-Oct-16 1:54 
QuestionView to Controller - HTML form POST action failing, with wrong Requested URL Pin
Member 1282452930-Oct-16 21:27
Member 1282452930-Oct-16 21:27 
AnswerRe: View to Controller - HTML form POST action failing, with wrong Requested URL Pin
Richard Deeming31-Oct-16 4:10
mveRichard Deeming31-Oct-16 4:10 
QuestionHow to show/hide textboxes based on what is selected via toggle switch Pin
Bootzilla3328-Oct-16 6:37
Bootzilla3328-Oct-16 6:37 
AnswerRe: How to show/hide textboxes based on what is selected via toggle switch Pin
Richard Deeming28-Oct-16 7:12
mveRichard Deeming28-Oct-16 7:12 
QuestionHow do I insert a toggle switch value into sql database Pin
Bootzilla3326-Oct-16 19:07
Bootzilla3326-Oct-16 19:07 
AnswerRe: How do I insert a toggle switch value into sql database Pin
Richard Deeming27-Oct-16 2:15
mveRichard Deeming27-Oct-16 2:15 
QuestionHow to submit a record and send as an email attachment at once? Pin
samflex26-Oct-16 9:43
samflex26-Oct-16 9:43 
AnswerRe: How to submit a record and send as an email attachment at once? Pin
ZurdoDev26-Oct-16 9:49
professionalZurdoDev26-Oct-16 9:49 
GeneralRe: How to submit a record and send as an email attachment at once? Pin
samflex26-Oct-16 10:34
samflex26-Oct-16 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.