缺少验证表单的方法例程

时间:2017-05-16 13:39:34

标签: c# asp.net-mvc

我想在单击“保存”按钮时显示带有验证消息的模态窗口,并将表单保存在数据库中。

Imex课程

using System;
using System.Collections.Generic;
using WebApplication1.Models;
using System.Linq;
using System.Web;
using WebApplication1.App_Data.DTOs; 

namespace WebApplication1.App_Data.DTOs
{
    public class DataMessageDTO
    {
        public bool IsValid { get; set; }

        public bool IsInfo { get; set; }

        public string Message { get; set; }
    }
    public class ImexReportDTO
    {

        public ImexReportDTO(string question, int questionId, int opSpeId)
        {
            this.Id = 0;
            this.OpSpeId = opSpeId;
            this.Question = question;
            this.Response = "NON";
            this.Comment = null;
            this.Signatory = null;
            this.Date = null;
            this.QuestionId = questionId;
        }

        public ImexReportDTO(IMEXREPORTDEBUG k)
        {
            this.Id = k.Id;
            this.OpSpeId = k.OpSpeId;
            this.Question = k.Question;
            this.Response = k.Response;
            this.Comment = k.Comment;
            this.Signatory = k.Signatory;
            this.Date = k.Date;
            this.QuestionId = k.QuestionId;
        }

        public long Id { get; set; }

        public int OpSpeId { get; set; }

        public string Question { get; set; }

        public string Response { get; set; }

        public string Comment { get; set; }

        public string Signatory { get; set; }

        public DateTime? Date { get; set; }

        public int? QuestionId { get; set; }


    }
}

我的观点指数

 @model IEnumerable<WebApplication1.App_Data.DTOs.ImexReportDTO>
@Styles.Render("~/Content/themes/base/css", "~/Content/css")

    @{

        ViewBag.Title = "Index";
        var date = DateTime.Now.ToShortDateString();


    }

        <div class="form-horizontal">
            <h4 class="text-left">
                Bonjour, vous vous êtes bien connecté avec le numéro d'OT <span style="color:#077937">@(ViewBag.OT) </span>
            </h4>
        </div>
<div ajaxUpdateContainerId="gridContent" class="row">
    @using (Html.BeginForm("Valid","ImexReportDTOes", FormMethod.Post, new { id= ""}))
    {
        @Html.Hidden("returnUrl", Request.Url.ToString())

        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <fieldset>
            <section class="panel panel-default" style="align-content:center;text-align:center; background-color:#e9e7e7">
                <section class="panel-heading panel-success" style="align-content:center; text-align:center">
                    <div class="panel-heading" style="text-align:center">

                        <h2 style="color:#280202;display:inline-block;text-align:center;">
                            Veuillez remplir le questionnaire pour le site "..." @(ViewBag.entite)
                        </h2>
                    </div>
                </section>
                <section class="panel-body" style="align-content:center; text-align:center">
                    <div class="form-group" style="align-content:center; text-align:center">
                        <div class="table-responsive" style="align-content:center; text-align:center">
                            <table id="table" class="=table table-responsive table-bordered table-hover nav nav-justifiedr" style="align-content:center; text-align:center; width:100%">

                                <thead class="panel-title bg-success" style="align-content:center">
                                    <tr>
                                        <th style="text-align:center">&nbsp;ID&nbsp;</th>
                                        <th style="text-align:center" class="control-label">&nbsp;OpSpeID&nbsp; </th>
                                        <th style="text-align:center" class="control-label">&nbsp;QUESTION&nbsp; </th>
                                        <th style="text-align:center" class="control-label">&nbsp;REPONSE&nbsp; </th>

                                        <th style="text-align:center" class="control-label"> &nbsp;COMMENTAIRE&nbsp; </th>
                                        <th style="text-align:center" class="control-label">&nbsp;SIGNATAIRE&nbsp;</th>
                                        <th style="text-align:center" class="control-label">&nbsp;QuestionId&nbsp;</th>
                                        <th style="text-align:center" class="control-label">&nbsp;DATE&nbsp;</th>
                                    </tr>
                                </thead>
                                <tbody class="panel-body">
                                    @foreach (var item in Model)
                                    {

                                        <tr>
                                            <td class="panel-heading bg-success">
                                                @Html.DisplayFor(modelItem => item.Id)
                                                @Html.ValidationMessageFor(modelItem => item.Id)
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.OpSpeId, new { htmlAttributes = new { @class = "form-control", @rows = "3" } })
                                                @Html.ValidationMessageFor(modelItem => item.OpSpeId, "", new { @class = "form-group" })
                                            </td>
                                            <td>
                                                @Html.DisplayFor(modelItem => item.Question, new { htmlAttributes = new { @class = "form-control", @rows = "3" } })
                                                @Html.ValidationMessageFor(modelItem => item.Question, "", new { @class = "form-group" })
                                            </td>
                                            <td>

                                                <select>
                                                    @{
                                                        if (item.Response == "NON")
                                                        {
                                                            <option> @Html.DisplayFor(modelItem => item.Response, new { htmlAttributes = new { @class = "form-control", @rows = "3" } }) </option>
                                                            <option value="OUI"> OUI </option>
                                                            <option value="Sans Objet"> SANS OBJET </option>
                                                        }

                                                        else if (item.Response == "OUI")
                                                        {
                                                            <option> @Html.DisplayFor(modelItem => item.Response, new { htmlAttributes = new { @class = "form-control", @rows = "3" } }) </option>
                                                            <option value="NON"> NON </option>
                                                            <option value="Sans Objet"> SANS OBJET</option>
                                                        }
                                                        else
                                                        {
                                                            <option> @Html.DisplayFor(modelItem => item.Response, new { htmlAttributes = new { @class = "form-control", @rows = "3" } }) </option>
                                                            <option value="NON"> NON </option>
                                                            <option value="OUI"> OUI </option>
                                                        }
                                                    }

                                                </select>
                                                @Html.ValidationMessageFor(modelItem => item.Response, "", new { @class = "form-group text-danger" })
                                            </td>
                                            <td>
                                                @Html.TextAreaFor(model => item.Comment, new { htmlAttributes = new { @class = "form-control", @rows = "3", @textMode = "Multiline", @id = "Comment" } })
                                                @Html.ValidationMessageFor(modelItem => item.Comment, "", new { @class = "form-group text-danger", @id = "Comment" })
                                            </td>
                                            <td>
                                                @Html.DisplayFor(model => item.Signatory, new { htmlAttributes = new { @class = "form-control", @rows = "3" } })
                                                @Html.ValidationMessageFor(modelItem => item.Signatory, "", new { @class = "form-group" })
                                            </td>
                                            <td>
                                                @Html.DisplayFor(model => item.QuestionId, new { htmlAttributes = new { @class = "form-control", @rows = "3" } })
                                                @Html.ValidationMessageFor(modelItem => item.QuestionId, "", new { @class = "form-group" })
                                            </td>
                                            <td>
                                                @date
                                                @Html.ValidationMessageFor(modelItem => item.Date, "", new { @class = "form-group" })
                                            </td>
                                        </tr>
                                                        }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </section>
                <section class="panel-footer panel-success">
                    <div class="form-group panel-success" style="text-align:center">

                        <button type="submit" class="form-group modal-link btn btn-success" id="enregistrer" value="enregistrer" data-toggle="modal" data-target="#modal-container"> <span class="glyphicon glyphicon-floppy-saved"></span> Enregister</button>
                        <button type="reset" class="form-group  btn btn-danger"> <span class="glyphicon glyphicon-floppy-remove"></span> Annuler</button>

                    </div>
                </section>

            </section>
        </fieldset>
                                                        }
</div>

    <!-- Modal -->
<div id="modal-container" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding-top:100px">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <div class="modal-title" id="myModalLabel"><h4>Message Modal</h4></div>
            </div>
            <div class="modal-body">
                @{
                    if (ViewBag.Comment == "")
                    {
                        <h4>
                            Veuillez remplir le champs Commentaire SVP
                        </h4>
                    }
                    else
                    {

                        <h4>
                            <span style="align-content:center ;color:darkorange"> Merci ! </span> Votre Questionnaire a bien était enregistré.
                        </h4>
                    }
                }
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div><!-- /.modal -->


@section Scripts {
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@Styles.Render("~/Content/bootstrap")
@Styles.Render("~/Content/bootstrap.min.css")
@Styles.Render("~/Content/css")
}

Mon控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Xml;
using System.Web.UI.WebControls;
using WebApplication1.App_Data.DTOs;
using System.Web.Mvc;
using WebApplication1.App_Data.Repositories;
using WebApplication1.Models;
using FlashMessage;

namespace WebApplication1.Controllers
{
    public class ImexReportDTOesController : Controller
    {
        private WebApplication1Context db = new WebApplication1Context();


        // GET: ImexReportDTOesController
        [HttpGet]
        public ActionResult Index(
            string OT,
            string entite,
            string Comment
            )
        {

            ViewBag.user = Repository.GetLogonUser();
            ViewBag.OT = OT;
            ViewBag.entite = entite;
            int id = 0;
            ViewBag.Comment = Comment;

            if (OT != null && OT.Length > 0)
            {

                if (int.TryParse(OT, out id))
                {
                    var x = Repository.FindImexReport(id);


                    List<OpSpeDTO> opSpes = Repository.ExportOpSpe(id);

                    if (opSpes.Any() == false)
                    {
                    }
                    else if (opSpes.First().cas == null)
                    {
                    }
                    else
                    {
                        OpSpeDTO opSpe = opSpes.First();

                        if (opSpe.cas == 'A') opSpe.cas = '1';
                        if (opSpe.cas == 'B') opSpe.cas = '2';
                        if (opSpe.cas == 'C') opSpe.cas = '3';

                        if (opSpe.cas != '1' && opSpe.cas != '2' && opSpe.cas != '3' && opSpe.cas != '4' && opSpe.cas != '5')
                        {
                        }
                        else
                        {
                            if (x != null)
                            {

                                return View(Repository.FindImexReport(id));
                            }
                            else
                            {
                                return null;
                            }

                        }

                    }
                }
            }
           ViewBag.MessageErreur = "Le Numéro d'OT que vous avez saisi n'est pas correct !";
            return View("Error");
        }

     public PartialViewResult Valid()
      {
           List<string> errors;
           List<ImexReportDTO> reports = new List<ImexReportDTO>();
          var result = Repository.UpdateReport(reports, out errors);

          return PartialView( "Index", result);
       }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public PartialViewResult Valid(ImexReportDTO imexReportDTO)
        {

            List<string> errors;
            List<ImexReportDTO> reports = new List<ImexReportDTO>();
            DataMessageDTO dataMessage = new DataMessageDTO();
            if (ModelState.IsValid)
            { 
                Repository.UpdateReport(reports, out errors);
                dataMessage.IsValid = true;
                dataMessage.Message = "saisie enregistre";
            }
            else
            {
                dataMessage.Message = "erreur";
            }
            ModelState.Clear();
            return PartialView("_DataMessage", dataMessage);
        }
        //   

        //  public ActionResult Index([Bind(Include = "Id,OpSpeId,Question,Response,Comment,Signatory,QuestionId")] ImexReportDTO imexReportDTO)
        //     {
        //      List<string> errors;
        //
        //       string user = Repository.GetLogonUser();

        //      List<ImexReportDTO> reports = new List<ImexReportDTO>();



        //       if (ModelState.IsValid)
        //     {


        //        Repository.UpdateReport(reports, out errors);
        //        this.db.ImexReportDTOes.Add(imexReportDTO);
        //     this.db.SaveChanges();
        //TempData["Msg"] = "Votre Questionnaire a bien était enregistré.";
        //       return this.RedirectToAction("Index");
        //    }

        //  this.ViewBag.Response = new SelectList(this.db.ImexReportDTOes, "Response", "", imexReportDTO.Response);

        //          return this.View(imexReportDTO);


        //  }


    }
}

1 个答案:

答案 0 :(得分:0)

 <script type="text/javascript">
    $(document).ready(function () {
        $(".btn.btn-danger").click(function (e) {
            e.preventDefault();
            var id = $(this).data("id");
            swal({
                title: "Are you sure?",
                text: "This record will be saved!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete it!",
                closeOnConfirm: false
            },
            function (result) {
                if (result) {
                    swal("Saved!", "This record has be Saved.", "success");
                    $("form#" + id).submit();
                }
            });
        });
    });
</script>
  

你可以像这样使用jQuery。   &#34; http://t4t5.github.io/sweetalert/&#34;   我认为这将有助于开源