简体   繁体   English

ASP.NET MVC 获取输入 id 到控制器以在将模型保存到数据库之前进行一些更改

[英]ASP.NET MVC get input id to controller to make some changes before save model in database

在此处输入图片说明 i get date from cshtml as [StartDate - EndDate] and the date in data base is split as start date as field and end date i want to get this date in controller and split them before saveChanges in data base我从 cshtml 获取日期为 [StartDate - EndDate] 并且数据库中的日期被拆分为开始日期作为字段和结束日期我想在控制器中获取此日期并在数据库中的 saveChanges 之前拆分它们

cshtml code : cshtml 代码:

    <div>        
       <input type="text" value='@Model.StartDate - @Model.EndDate' class="form-control pull-right" id="reservationtime" >
   </div>

and controller code :和控制器代码:

  public ActionResult Edit([Bind(Include = "PerformanceId,EventId,VenueId,Score,Name,Image,Description,StartDate,EndDate,Facebook,Website,Views,IsClosedBooking,IsVisible,IsFeatured,Deleted,CreatedOn,CreatedBy,ModifiedOn,ModifiedBy")] Performance performance)
    {
      if (ModelState.IsValid)
        {
            db.Entry(performance).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
  return View(performance);
    }

Create two hidden fields, one with name StartDate and other with name EndDate and assign the values accordingly.创建两个隐藏字段,一个名为StartDate ,另一个名为EndDate并相应地分配值。 These values will be available in the controller when you post the form.当您发布表单时,这些值将在控制器中可用。

<input type="hidden" name="StartDate" value="@Model.StartDate" />
<input type="hidden" name="EndDate" value="@Model.EndDate" />

i reach to my answer i will use the event of daterangepaker when i click apply i will split the date我得到了我的答案 我将使用 daterangepaker 事件,当我单击应用时,我将拆分日期

$('#daterange').daterangepicker();
$('#daterange').on('apply.daterangepicker', function(ev, picker) {
   var date = document.getElementById("reservationtime").value;
var res = date.split("-");
document.getElementById("startDate").value = res[0];
document.getElementById("endDate").value = res[1];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM