c# - How to submit into specific table, because i change two different context , 1 is translate bar and 1 is submit form -
"Hello i am student i am using c# and asp.net, i have a function that i want to submit a form, while using saveChange it will save all change, in my function i translate data in another context so when i submit form using savechange it will save that translate into database, do you have any idea on this, thank you public async Task<IActionResult> Registration(IFormCollection form) { ViewBag.Lang = await _context.Languages.ToListAsync(); int langId = (Request.Cookies[""Language""] != null && Request.Cookies[""Language""] != """") ? Convert.ToInt32(Request.Cookies[""Language""] + """") : 1; ViewBag.langId = langId; ViewBag.Countries = await _context.Country.ToListAsync(); ViewBag.CurrentLang = await _context.Languages.Where(l => l.LangId == langId).FirstOrDefaultAsync(); SetCookie(""Language"", langId + """", 1); IList<Menu> MenuItems = await _context.Menu.OrderBy(p => p.Order).Where(m => m.MenuGroupId == 1).Include(s => s.SubMenu).ToListAsync(); foreach (Menu menu in MenuItems) { // Translate Submenu string trans = TranslateItem(langId, menu.GetType().ToString(), menu.MenuId, nameof(Menu.Name)); if (trans != null && trans.Trim(' ') != """") menu.Name = trans; foreach (SubMenu subMenu in menu.SubMenu) { trans = TranslateItem(langId, subMenu.GetType().ToString(), subMenu.SubMenuId, nameof(SubMenu.SubMenuName)); if (trans != null && trans.Trim(' ') != """") subMenu.SubMenuName = trans; } } ViewBag.MenuItems = MenuItems; IOrderedQueryable<OtherLinks> otherLinks = _context.OtherLinks.OrderBy(o => o.OtherLinkOrder); ViewBag.FooterLinks = otherLinks.Where(p => p.ShowOnFooter).ToList();
string uniqueFileName = """"; string dir = RandomString(12); string RandomName = RandomString(12); string base64 = Request.Form[""file_p""]; if (base64 == """") { IQueryable<Field> Tempfields = _context.Field .Include(m => m.Language) .Include(g => g.GroupFields) .Where(m => m.DeletedTimestamp == null && m.GroupFields.GroupFieldName == ""Media-Primary"") .OrderBy(p => p.FieldPage) .OrderBy(o => o.FieldOrder);
List<Field> tempfields = await Tempfields.ToListAsync(); ViewBag.formFields = tempfields;
ViewBag.message = ""NoPhoto""; return View(); } byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]); var filePath = Path.Combine(Directory.GetCurrentDirectory(), ""wwwroot"", ""uFiles\\uploads\\"", dir); var formFiles = form.Files;
if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } var fileNameWithPath = string.Concat(filePath, ""/"", RandomName, "".png""); uniqueFileName = string.Concat(uniqueFileName, ""uFiles/uploads/"", dir, ""/"", RandomName, "".png"", "";""); using (FileStream stream = new FileStream(fileNameWithPath, FileMode.Create)) { stream.Write(bytes, 0, bytes.Length); stream.Flush(); }
String name = Request.Form[""FirstName""] + Request.Form[""MiddleName""] + Request.Form[""LastName""]; String country = Request.Form[""Country""]; String orginization = Request.Form[""Organization""];
String result = uniqueFileName + ""<NewColumn>"" + Request.Form[""FirstName""] + ""<NewColumn>"" + Request.Form[""MiddleName""] + ""<NewColumn>"" + Request.Form[""LastName""] + ""<NewColumn>"" + Request.Form[""Dob""] + ""<NewColumn>"" + Request.Form[""Sex""] + ""<NewColumn>"" + Request.Form[""Phone""] + ""<NewColumn>"" + Request.Form[""Fax""] + ""<NewColumn>"" + Request.Form[""Email""]+ ""<NewColumn>"";
IQueryable<Field> fields = _context.Field .Include(m => m.Language) .Include(g => g.GroupFields) .Where(m => m.DeletedTimestamp == null && m.GroupFields.GroupFieldName == ""Media-Primary"") .OrderBy(p => p.FieldPage) .OrderBy(o => o.FieldOrder);
List<Field> formFields = await fields.ToListAsync(); ViewBag.formFields = formFields;
//check to prevend refresh submit old form bool isFormSubmit = result.Any(); if (isFormSubmit) { DateTime? timestamp = DateTime.Now;
// get form fields Microsoft.Extensions.Primitives.StringValues value = """" ; List<string> fieldValues = new List<string>();
//To Save dynamic fields foreach (Field field in formFields) {
form.TryGetValue(field.FieldName, out value); string v = value; result = String.Concat(result, v , ""<NewColumn>""); };
String final = result; Registration file = new Registration { Status = ""pending"", Name = name, Country = country, Organization = orginization, FormData = final, CreatedTimestamp = timestamp, UpdatedTimestamp = timestamp, };
_context.Add(file); await _context.SaveChangesAsync();
ViewBag.message = ""added"";
}
return View(); }
c# asp.net asp.net-mvc asp.net-core-3.1
ShareShare a link to this question Copy linkCC BY-SA 4.0
Follow Follow this question to receive notifications
asked 2 mins ago Slack_nySlack_ny 3911 silver badge44 bronze badges"
|