Saving Dynamic Form Fields to Database with Fixed Values in MVC

kadir 60 Reputation points
2024-05-23T07:34:52.57+00:00
    [HttpPost]

    [ValidateAntiForgeryToken]

    public ActionResult Create(EgitimSeminerViewModel model)

    {

        if (ModelState.IsValid)

        {

            for (int i = 0; i < model.EgitimSeminerBilgiList.Count; i++)

            {

                TBL_EgitimSeminerBilgi item = new TBL_EgitimSeminerBilgi();

                item.EgitimKonu = model.EgitimSeminerBilgiList[i].EgitimKonu;

                item.EgitimKurum = model.EgitimSeminerBilgiList[i].EgitimKurum;

                item.EgitimSuresi = model.EgitimSeminerBilgiList[i].EgitimSuresi;

                db.TBL_EgitimSeminerBilgi.Add(item);

            }

            db.SaveChanges();

            return RedirectToAction("Create"); // Or other redirection

        }

        return View(model);

    }

	Hello everyone, friends, my goal here is to record according to the number of incoming data, no problem until this part 

	but I also want to add the GeneralInfoid and value fields in the same table, so these fields will be fixed.

	item.GenelBilgiid = 12; item.deger = false; no matter how many times the data is added
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-05-23T08:33:23.7633333+00:00

    Hi @kadir,

    item.GenelBilgiid = 12; item.deger = false; no matter how many times the data is added

    Do you mean no matter how many times you add data, item.GenelBilgiid is always 12; item.deger is always false;?

    You can write directly into the for loop.

    for (int i = 0; i < model.EgitimSeminerBilgiList.Count; i++)
    {                  
        TBL_EgitimSeminerBilgi item = new TBL_EgitimSeminerBilgi();
        item.EgitimKonu = model.EgitimSeminerBilgiList[i].EgitimKonu;
        item.EgitimKurum = model.EgitimSeminerBilgiList[i].EgitimKurum;
        item.EgitimSuresi = model.EgitimSeminerBilgiList[i].EgitimSuresi;
        item.GenelBilgiid = 12;
        item.isDelete = false;
        db.TBL_EgitimSeminerBilgi.Add(item);
        
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.