Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Context.add not saving to database

$
0
0

Hi,

I have a controller which needs to pull the data from the form and add to database. I've done this numerous times before, but for some reason on this specific section of code it will not save at all.

I've got it pulling the info into the console.writeline and checked that it's all the correct info. I have also tried adding dummy data, but nothing will save. I've also tried using just _console.Add();, _console.Add(RACIResUser);, _context.RACIResUser.Add(RACIResUser), but nothing is saving. As I said, I can view it all the correct info in the console, and other parts of the form on the page are saving to a different table within the database, it's just this section that wont save.

Code is below: Post Controller:

[HttpPost]publicasyncTask<IActionResult>InstanceProcess(stringInstanceId,[Bind(Prefix="SectionInfo")]IEnumerable<ProcessOutput> secinf){var res =(from i in _context.SOPRACIResselectnewProcessOutput{SOPTemplateID= i.soptoptempid, valuematch = i.valuematch,JobTitleId= i.JobTitleId,DepartmentId= i.DepartmentId}).ToList();ViewBag.res = res;foreach(var item in secinf){foreach(var data in(ViewBag.res)){ApplicationDbContext.RACIResChosenUserRACIResUser=newApplicationDbContext.RACIResChosenUser();var sel =Request.Form[data.valuematch +"-RES"];Console.WriteLine("Dropdown res:");Console.WriteLine(sel);int onevalue =Convert.ToInt32(sel);var status ="Pending";RACIResUser.RACIResID= data.valuematch;RACIResUser.UserId= onevalue;RACIResUser.Status= status;RACIResUser.soptoptempid = data.SOPTemplateID;RACIResUser.InstanceId= getid;Console.WriteLine("data valuematch:");Console.WriteLine(RACIResUser.RACIResID);Console.WriteLine("data SOPTemplateID:");Console.WriteLine(data.SOPTemplateID);

          _context.RACIResUser.Add(RACIResUser);
          _context.SaveChanges();}}}

DB context:

publicclassRACIResChosenUser{[Key]publicintRACIResChosenID{get;set;}publicstringRACIResID{get;set;}publicstringStatus{get;set;}publicstringStatusComplete{get;set;}publicintUserId{get;set;}publicstring soptoptempid {get;set;}publicstringInstanceId{get;set;}}publicDbSet<RACIResChosenUser>RACIResUser{get;set;}

cshtml:

@foreach(var data in(ViewBag.res)){var dropname = item.valuematch +"-RES";<div class="col-12 col-md-6">@Html.DropDownList(@dropname,ViewBag.selectlist asSelectList,new{@class="form-control"})</div>}

</div>


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>