将List <t>中的值添加到数据库列

时间:2018-05-18 09:08:29

标签: c# asp.net-mvc database list

目前,我可以插入包含secondminutehourRepeatJobIdUserID的记录,但是,它只会从每个List<object>中插入一个值。

问题1: 如何将List<object>model.DayOfMonthmodel.Monthmodel.DaysOfWeek)中的值添加到本地数据库中?

问题2: 另外,据我了解,列表中的每个值都应该有自己的记录。在复制List<object>secondminutehour时,如何为repeat(上面命名)中的每个值创建新记录, JobIdUserID

控制器:

    [HttpPost]
    public ActionResult ScheduleInfo(Values model, int JobList1, string Second, string Minute, string Hour, object DayOfMonth, object Month, object DaysOfWeek, int repeatTime)
    {
        var secondCon = Convert.ToInt32(Second);
        var minuteCon = Convert.ToInt32(Minute);
        var hourCon = Convert.ToInt32(Hour);


        model.Job = JobList1;



        model.Second = secondCon;
        model.Minute = minuteCon;
        model.Hour = hourCon;
        model.DayOfMonth = new List<object>();
        model.Month= new List<object>();
        model.DaysOfWeek = new List<object>();



        foreach (var dofm in model.DofMInfo)
        {
            if (dofm.IsChecked)
            {
                model.DayOfMonth.Add(dofm.DofMID);
            }
        }

        foreach (var month in model.MonthInfo)
        {
            if (month.IsChecked)
            {
                model.Month.Add(month.monthID);
            }
        }

        foreach (var day in model.DayInfo)
        {
            if (day.IsChecked)
            {
                model.DaysOfWeek.Add(day.dayID);
            }
        }

        model.repeatTime = repeatTime;

        try
        {
            ScheduleEntity db = new ScheduleEntity();

            AspNetUser aspNetUser = new AspNetUser();
            Job job = new Job();

            Schedule sched = new Schedule();

            sched.Second = Convert.ToString(model.Second);
            sched.Minute = Convert.ToString(model.Minute);
            sched.Hour = Convert.ToString(model.Hour);


            //foreach (var day in model.DayOfMonth)
            //{
            //    sched.DayOfMonth = Convert.ToString(day);
            //}


            //foreach (var month in model.Month)
            //{
            //    sched.Month = Convert.ToString(month);
            //}


            //foreach (var weekday in model.DaysOfWeek)
            //{
            //    sched.DayOfWeek = Convert.ToString(weekday);
            //}


            sched.Repeat = 4;
            sched.JobId = 2;
            sched.AspNetUsersId = User.Identity.GetUserId();
            db.Schedules.Add(sched);
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            throw ex;
        }


        return RedirectToAction("SchedulerIndex");
    }
}

我正在调用的Schedule类并设置为sched

using System;
using System.Collections.Generic;

public partial class Schedule
{
    public int ScheduleID { get; set; }
    public string Second { get; set; }
    public string Minute { get; set; }
    public string Hour { get; set; }
    public string DayOfMonth { get; set; }
    public string Month { get; set; }
    public string DayOfWeek { get; set; }
    public string AspNetUsersId { get; set; }
    public Nullable<int> JobId { get; set; }
    public int Repeat { get; set; }

    public virtual AspNetUser AspNetUser { get; set; }
    public virtual Job Job { get; set; }
}

0 个答案:

没有答案