Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public ActionResult Index()
    {
        List<SubLIst> nodes = new List<SubLIst>();
    dbEntities entities = new dbEntities();

        //Loop and add the Parent Nodes.
        foreach (tblManual type in entities.tblManualLists)
        {
            nodes.Add(new SubLIst { id = type.ManualId.ToString(), parent = "#", text = type.Code });
        }

        //Loop and add the Child Nodes.
        foreach (tblSubManual subType in entities.tblSubManuals)
        {
            nodes.Add(new SubLIst { id = subType.ManualId.ToString() + "-" + subType.SubManualId.ToString(), parent = subType.ManualId.ToString(), text = subType.Code });
        }

        //Serialize to JSON string.
        ViewBag.Json = (new JavaScriptSerializer()).Serialize(nodes);
        return View();
    }

    [HttpPost]
    public ActionResult Index(string selectedItems, tblDocument employee)
    {

        if (ModelState.IsValid)
        {
            List<SubLIst> items = (new JavaScriptSerializer()).Deserialize<List<SubLIst>>(selectedItems);
            employee.ModifiedDate = DateTime.Now;

            dc.tblDocuments.Add(employee);
            dc.SaveChanges();
            return RedirectToAction("Index");
        }
        return Json(new { success = true }, JsonRequestBehavior.AllowGet);
    }
}


What I have tried:

the insert statement conflicted with the foreign key constraint fk_The statement has been terminated.
Posted
Updated 2-Feb-21 0:21am
v3

1 solution

You are trying to insert a record into your table which references another record which does not exist.

We can't see your database or your model classes, and you've truncated the error message, so we can't tell you where the problem is. At a guess, it could be the parent = "#" line - but that is just a guess.
 
Share this answer
 
Comments
Member 15062227 2-Feb-21 6:25am    
if (ModelState.IsValid)
{
List<sublist> items = (new JavaScriptSerializer()).Deserialize<list<sublist>>(selectedItems);
employee.SubManualId = Convert.ToInt32(items[0].id);
entities.tblDocuments.Add(employee);
entities.SaveChanges();


return RedirectToAction("Index");
}
return Json(new { success = true }, JsonRequestBehavior.AllowGet);


I am not paasing id selecteditem that i got error..Now i solved my error

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