将数据从View写入数据库中的每一行

时间:2017-03-09 14:46:27

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

我有这样的模特

 public class InvitationMails
{
    [Key]
    public int Individ_Id { get; set; }
    public string From { get; set; }
    [Display (Name = "Почта")]
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public string Link { get; set; }
    public string Name { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? LinkStart { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? LinkEnd { get; set; }


}

我有datepickers,我输入日期。 我也在桌上发了电子邮件,因为我把它写在其他视图上。

这是视图

 <div class="inner-div3">
    <div class="right-welcome-div2">
        <div class="counterdiv3">
            <div style="float: left; width: 100%; height: 100%; padding-top: 5px; background: lightgray">
                <b style="text-align: center; font-size: 20px;">Время жизни ссылки</b>
            </div>

        </div>
        <div style="width: 100%; height: 25%;">
            <div style="margin-left: 50px; float: left; margin-top: 20px; height: 25%;">
                <div style="margin-bottom: 20px;">
                    <b>Время начала </b>
                    <input type="text" class="form-control" id="date" placeholder="Время окончания">
                </div>

            </div>
            <div style="margin-right: 50px; float: right; margin-top: 20px; height: 25%;">
                <div style="margin-bottom: 20px;">
                    <b>Время окончания </b>
                    <input type="text" class="form-control" id="date2" placeholder="Время окончания">
                </div>

            </div>

        </div>
        <div style="width: 100%; height: 23%;">
            @Html.TextBoxFor(model => model.First().Link, new { @class = "datetimepicker form-control", placeholder = "Ссылка", @style = "margin-top:35px;",id = "date" })
        </div>
        <div style="text-align: center">
            <input type="submit" value="Генерировать" style="font-size: 16px" class="btn btn-default" />
        </div>
    </div>

    <div class="left-welcome-div2">
        <div class="counterdiv2">
            <b style="text-align: center; font-size: 20px;">Список приглашений</b>
        </div>
        <div style="height: 60%; width: 100%; overflow: auto">
            @{int rowNo = 0;}
            @foreach (var item in Model)
            {
                <div style="margin-left: 25px; margin-top: 10px;">
                    <b>@(rowNo += 1)</b>
                    <b style="margin-left: 10px;">@Html.DisplayFor(modelItem => item.To)</b>
                    <a style="float: right; margin-right: 20px;" href='@Url.Action("Edit", "InvitationMails", new {id = item.Individ_Id})'>
                        <img src='@Url.Content("~/Images/Edit.png")' />
                    </a>
                    <a style="float: right; margin-right: 20px;" href='@Url.Action("Delete", "InvitationMails", new {id = item.Individ_Id})'>
                        <img src='@Url.Content("~/Images/Delete.png")' />
                    </a>
                </div>
            }
        </div>
        <div style="height: 20%; width: 100%">
            <ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 20px">
                <li style="color: white">@Html.ActionLink("Добавить почту", "Create", "InvitationMails", null, new { @style = "color:white" })</li>
            </ul>
            <ul class="btn btn-default" style="width: 150px; list-style-type: none; font-size: 16px; margin-left: 230px">
                <li style="color: white">@Html.ActionLink("Отправить", "SendEmail", "InvitationMails", null, new { @style = "color:white" })</li>
            </ul>
        </div>

    </div>
</div>

<script>
   $('#date').datepicker({
            format: "dd/mm/yyyy"
   });
   $('#date2').datepicker({
       format: "dd/mm/yyyy"
   });
   </script>

我需要从日期选择器中取出日期,并在点击此按钮时输入表格中的所有行<li style="color: white">@Html.ActionLink("Отправить", "SendEmail", "InvitationMails", null, new { @style = "color:white" })</li>

我怎么能这样做?

感谢&#39; s很多帮助

更新

我写这样的方法,但它用静态数据更新表。

如何将datepickers的数据传输到此方法?

   public static void UpdateLinkStart()
    {
        ApplicationDbContext context = new ApplicationDbContext();

        IEnumerable<InvitationMails> customers = context.InvitationMails
            .Where(c => c.Link == null)
            .AsEnumerable()
            .Select(c => {
                c.Link = "test.com";
                return c;
            });
        foreach (InvitationMails customer in customers)
        {

            context.Entry(customer).State = EntityState.Modified;
        }



        context.SaveChanges();

    }

0 个答案:

没有答案