如何在视图和控制器之间传递数据?

时间:2018-06-27 08:19:54

标签: javascript jquery html asp.net-mvc controller

这是我们的房屋指数:

@model ELearning.Data.ELearningEgitimDTO
@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    @if (TempData["message"] != null)
    {
        <div class="alert alert-info" role="alert">@TempData["message"]</div>
    }
    <div>
        <div>
            @if (Model.EgitimTuru == 5)
            {
                <h1>Yangın Eğitimi</h1>
            }

            <table class="table table-responsive">
                <tr>
                    <td width="20%">Şirket Adı:</td>
                    <td width="80%">@Model.Name</td>
                </tr>
                <tr>
                    <td>Eğitimi Veren:</td>
                    <td>@Model.PersonelAdi</td>
                </tr>
                <tr>
                    <td>Eğitim Tarihi:</td>
                    <td>@Model.Tarih</td>
                </tr>
            </table>
        </div>
        <div>
            <table class="table table-responsive">
                <tr>
                    <td>Eğitim Konuları:</td>
                </tr>
                @foreach (var konu in Model.Adi)
                {
                    <tr>
                        <td><ul><li>@konu</li></ul></td>
                    </tr>
                }

            </table>
        </div>
    </div>
    <p class="text-right"><button onclick="getStartDate()" class="btn btn-primary btn-lg ">Eğitime Başla »</button></p>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>

    <script type="text/javascript">

        function getStartDate() {
            
            $.post("/Video/GetStartDate",
                {
                    PerNr: @Model.PerNr,
                    StartDate: "",
                    EndDate: "",
                    EgitimFilesId: @Model.FileId
                });
        }
    </script>
</div>

这是视频索引:

@model WebApplication3.Models.VideoLogsModel
@{
    ViewBag.Title = "Video Page";
}

<div class="jumbotron">
    <div>Eğitime başlanan zaman:</div>
    <div id="startdate"></div>
    <div class="col-md-12 text-center">
        <video controls controlslist="nodownload" id="videoPlayer" width: 100% height: auto>
            <source src="~/Video/GetVideo" type="video/mp4">
        </video>
    </div>
    <br />
    <div class="text-right">
        <p id="button" onclick="egitimiBitir()" class="btn btn-danger btn-lg ">Eğitimi Bitir</p>
    </div>
    <script type="text/javascript">
        var vid = document.getElementById("videoPlayer");
        var button = document.getElementById("button");

        if (vid.played) {
            setInterval(function () { vid.pause(); }, 30000);
        }
        vid.addEventListener("ended", function() {
            button.className = "btn btn-success btn-lg "
        });

        function egitimiBitir() {

            if (vid.ended) {
                $.post("/Video/GetEndDate",
                    {
                        PerNr: @Model.PerNr,
                        StartDate= "",
                        EndDate: "",
                        EgitimFile sId: @Model.EgitimFilesId
                    });
            }
            else {
                document.getElementById("message").innerHTML = "Video tamamlanmadan eğitimi bitiremezsiniz.."
            }
        }
    </script>
</div>

这是我们的主要模型:

public class ELearningEgitimDTO
{
    public ELearningEgitimDTO() { }
    public ELearningEgitimDTO(string PerNr, int ID) 
    {
        this.ID = ID;
        this.PerNr = PerNr;
    }

    public int ID { get; set; }
    public string PerNr { get; set; }//katılımcıID
    public string Name { get; set; }//şirket adı
    public int EgitimTuru { get; set; }
    public DateTime Tarih { get; set; }//egitim tarihi
    public string PersonelAdi { get; set; } // eğitimi veren

    public int FileId { get; set; }
    public string FileName { get; set; }
    public string[] Adi { get; set; }
}

这是我们的模型:

public class VideoLogsModel
{
    public int EgitimFilesId { get; set; }
    public int PerNr { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }

}

这是我们的家庭控制器:

public class HomeController : Controller
{
    public ActionResult Index(int EgitimId=4, string PerNr="2")
    {
        ELearningService service = new ELearningService();
        ELearningEgitimDTO egitim = new ELearningEgitimDTO(PerNr, EgitimId);
        return View(service.getInfo(egitim));   //eğitim bilgileri istenirken egitimId ve egitim kullanıcıdaki  eğitmenin perNr si verilmeli!!
    }
}

这是我们的视频控制器:

public class VideoController : Controller
{ 

    public ActionResult Index(VideoLogsModel model)
    {
        return View(model);
    }
    public ActionResult GetVideo()
    {
        var memoryStream = new MemoryStream(System.IO.File.ReadAllBytes(@"C:\Users\cyare\Desktop\videoplayback.mp4"));
        //byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\melik.DESKTOP-LQQAB68\Desktop\videoplayback.mp4");
        //System.IO.File.WriteAllBytes(@"C:\Users\melik.DESKTOP-LQQAB68\Desktop\videoplayback.mp4", bytes);
        return new FileStreamResult(memoryStream, "video/mp4");
    }
    /* [HttpPost]
     public ActionResult GetStartEndDate(VideoLogsModel logs)
     {
         DateTime startDate = logs.StartDate; //database de uygun tabloya yazılır
         return RedirectToAction("Index", "Video");
     }*/
    [HttpPost]
    public ActionResult GetStartDate(VideoLogsModel model)
    {
        model.StartDate = System.DateTime.Now;
        ELearningDosyaKullaniciDTO user = new ELearningDosyaKullaniciDTO();
        user.egitimFileID = model.EgitimFilesId;
        user.egitimKullanıcı = model.PerNr;
        user.startDate = model.StartDate;
        ELearningService service = new ELearningService();
        //service.CreateLogs(user);
        //return RedirectToAction("Index","Video",model);*/
        return RedirectToAction("Index", model);
    }
    [HttpPost]
    public ActionResult GetEndDate(VideoLogsModel model)
    {
        model.EndDate = System.DateTime.Now;
        ELearningDosyaKullaniciDTO user = new ELearningDosyaKullaniciDTO();
        user.egitimFileID = model.EgitimFilesId;
        user.egitimKullanıcı = model.PerNr;
        user.endDate = model.EndDate;
        ELearningService service = new ELearningService();
        service.UpdateLogs(user);
        TempData.Add("message", String.Format("Eğitiminiz Tamamlanmıştır!"));
        return RedirectToAction("Index", "Home");
    }
}

我的问题是如何将模型从家庭索引传递到视频控制器,然后再传递到视频索引?

视频索引未运行。它进入视频索引,但随后再次运行主索引。

此外,它在按钮的onclick函数之前运行egitimibitir()函数。

1 个答案:

答案 0 :(得分:1)

您将ajax请求发送到您的服务。 (端点=> GetEndDate)我认为您可以像这样更改代码,

 $.ajax({
         type: "POST",
         url: "/Video/GetEndDate",  //your reqeust url
         contentType: "application/json; charset=utf-8",
         data: JSON.stringify({
               // your data here
         }),
         success: function (data) {
               // check state of data
               // after check window.location = // redirect url
         },
         error: function (data) {
               // handle exception 
         }
 });

您可以将控制器方法更改为数据控制器。 (不是ActionResult。创建一个包含您的数据和成功状态的自定义结果对象。您可以在每个ajax请求返回状态和数据时使用此自定义结果对象)。如果执行“ GetEndDate”方法时发生异常,则需要处理异常并将其显示给客户端。您将异常或成功完成响应发送给客户端(视图)。 (您可以在ajax错误中处理异常数据:函数)