question

MansoorAhmed-0534 avatar image
0 Votes"
MansoorAhmed-0534 asked YijingSun-MSFT answered

Feedback Submitting Without Validation

Hello Experts, I am New In MVC RAZOR. So Require Some Help.

PLEASE HELP

I Want To Validate Fields At The Time Of Submit Right Now It Shows Validation Error But The (ALERT) Says Form Successfully Saved

Here Is The Controller From Where I Am Calling And Saving DATA.




 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Web.Http;
 using System.Data.SqlClient;
 using System.Web;
 using System.Web.Mvc;
 using HotelCustSurvey.Models;
 using HotelCustSurvey.Service;
 using Newtonsoft.Json;
    
 namespace HotelCustSurvey.Controllers
 {
     [System.Web.Http.RoutePrefix("Review")]
     public class CustomerReviewController : ApiController
     {
         ModelCustSurvey rvModel = new ModelCustSurvey();
         CustomerReview rv = new CustomerReview();
         // GET: CustomerReview
         [System.Web.Mvc.HttpGet]
         [System.Web.Mvc.Route("Get")]
         public HttpResponseMessage Get()
         {
             //    SaveDeviceInFormation(data);
             string result = "service running. " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ", 2021-05-27-v0.1";
             var resp = new HttpResponseMessage(HttpStatusCode.OK);
             resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
             return resp;
         }
    
         [System.Web.Http.HttpGet]
         [System.Web.Http.Route("checkdb")]
         public string checkdb()
         {
             string result = "";
             try
             {
                 string connectionString = DbConnection.ConnectionString;
                 using (SqlConnection saveConnection = new SqlConnection(connectionString))
                 {
                     saveConnection.Open();
                     result = "DB connection successful";
                 }
             }
             catch (Exception ex)
             {
                 result = ex.Message;
             }
             return result;
         }
            
         [System.Web.Http.HttpPost]
         [System.Web.Http.Route("SaveReview")]
         public string SaveReview(ModelCustSurvey rev)
         {
    
           string result = "0";
             try
             {
                 result = rv.SaveCustomerReview(rev);
             }
             catch (Exception ex)
             {
                 result = ex.Message;
                    
             }
             return result;
         }
    
         [System.Web.Mvc.AllowAnonymous]
         [System.Web.Http.HttpPost]
         [System.Web.Http.Route("PostReviewData")]
         public string PostReviewData(ModelCustSurvey rev)
         {
         
    
             string result = "-1";
             try
             {
                 result = rv.PostReviewData(rev);
             }
             catch (Exception ex)
             {
                 result = ex.Message;
    
             }
             return result;
         }
    
         [System.Web.Mvc.AllowAnonymous]
         [System.Web.Http.HttpGet]
         [System.Web.Http.Route("GetReviewMainData")]
         public HttpResponseMessage GetReviewMainData(long ReservationNumber, string RoomNumber, string UniqueCode)
         {
             try
             {
                 var response = new HttpResponseMessage(HttpStatusCode.OK);
                 response.Content = new StringContent(JsonConvert.SerializeObject(rv.GetReviewMainData(ReservationNumber, RoomNumber, UniqueCode)));
                 response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                 return response;
    
             }  ////
             catch (Exception ex)
             {
                 return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
             }
         }
           
          
     }
 }




Below Is My MODEL



 using System;
 using System.ComponentModel.DataAnnotations;
    
 namespace HotelCustSurvey.Models
 {
   public class ModelCustSurvey
   {
     public long ReservationNumber { get; set; }
     public string RoomNumber { get; set; }
     public DateTime CheckinDate { get; set; }
     public DateTime CheckOutDate { get; set; }
     public string GuestName { get; set; }
     public string MobileNumber { get; set; }
     public long HotelId { get; set; }
     public string EmailId { get; set; }
     public byte IsFirstVisit { get; set; }  //1 yes or 0 no
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte OperationReceiption { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte ReceiptionLoby { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte BagDeliveryService { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte BusinessCenter { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte CoffeShop { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte Mosque { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte Restaurent { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte QualityofFood { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte FurnitureinRoom { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte LevelofCleanless { get; set; }  //1-5
    
     [Required]
     [Range(1, 5, ErrorMessage = "*")]
     public byte RoomService { get; set; }  //1-5
        
     public string FeedBack { get; set; }
     public string HotelName { get; set; }
     public string UserId { get; set; }
     public string ApiKey { get; set; }
     public string UniqueCode { get; set; }
   }
 }



Here Is My VIEW

 @{
     Layout = null;
 }
    
 @{
     ViewBag.Title = "CustSurvey";
 }
 @using HotelCustSurvey.Models
 @model HotelCustSurvey.Models.ModelCustSurvey
 @using HotelCustSurvey.Service
    
    
    
 <!DOCTYPE html>
    
 <html>
 <head>
     <meta name="viewport" content="width=device-width" />
     <title></title>
     <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
     <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
     <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
     <link href="~/Content/star-rating.css" rel="stylesheet" />
     <!------ Include the above in your HEAD tag ---------->
    
     <style>
    
         html, body {
             font-family: Tajawal, sans-serif;
             font-weight: bold;
         }
    
         /*.validation-summary-valid {
             display: none;
         }
    
         .field-validation-error{
             display:none;
         }*/
    
         .hiddenField {
             display: none;
         }
    
         .thankyou-page ._header {
             background: #fee028;
             padding: 25px 30px;
             text-align: center;
         }
    
             .thankyou-page ._header .logo {
                 max-width: 125px;
                 margin: 0 auto 10px;
             }
    
                 .thankyou-page ._header .logo img {
                     width: 100%;
                 }
    
             .thankyou-page ._header h1 {
                 font-size: 24px;
                 font-weight: 800;
                 color: white;
                 margin: 0;                
             }
    
         .thankyou-page ._body {
             /*margin: -70px 0 30px;*/
             margin: 30px 0 30px;
         }
    
             .thankyou-page ._body ._box {
                 margin: auto;
                 max-width: 90%;
                 padding: 10px;
                 background: white;
                 border-radius: 3px;
                 box-shadow: 0 0 35px rgba(10, 10, 10,0.12);
                 -moz-box-shadow: 0 0 35px rgba(10, 10, 10,0.12);
                 -webkit-box-shadow: 0 0 35px rgba(10, 10, 10,0.12);
             }
    
                 .thankyou-page ._body ._box h2 {
                     font-size: 32px;
                     font-weight: 600;
                     color: #4ab74a;
                 }
    
         .thankyou-page ._footer {
             text-align: center;
             padding: 50px 30px;
         }
    
             .thankyou-page ._footer .btn {
                 background: #4ab74a;
                 color: white;
                 border: 0;
                 font-size: 14px;
                 font-weight: 600;
                 border-radius: 0;
                 letter-spacing: 0.8px;
                 padding: 20px 33px;
                 text-transform: uppercase;
             }
     </style>
    
    
    
 </head>
 <body>
    
     @using (Html.BeginForm("CustomerReview", "CustomerReview", FormMethod.Post))    
     {
         @Html.ValidationSummary(true)
         @Html.TextBoxFor(mode => Model.ReservationNumber, new { @class = "hiddenField" })
         @Html.TextBoxFor(mode => Model.RoomNumber, new { @class = "hiddenField" })
         @Html.TextBoxFor(mode => Model.UniqueCode, new { @class = "hiddenField" })
    
         <div>
             <div class="thankyou-page">
                 <div class="_header">
                     <div class="logo">
                         <img src="~/Images/logo_Hotel.png" alt="Marei Bin Mahfouz Group Of Hotels" />
                     </div>                    
                 </div>
                 <div class="_body">
                     <div class="_box">
    
                         <div class="form-horizontal">
                             <fieldset>
                                 <legend class="text-center mb-3">Feedback Form / نموذج الملاحظات</legend>
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             @*@Html.LabelFor(model => model.GuestName, new { @class = "control-label col-md-4" })*@
                                             <label class="control-label col-md-4">Guest Name / اسم النزيل</label>
                                             <div class="col-md-8">
                                                 @Html.TextBoxFor(model => model.GuestName, new { @class = "form-control", @readonly = "readonly" })
                                                 @*@Html.ValidationMessageFor(model => model.GuestName, "", new { @class = "badge badge-danger" })*@
                                             </div>
                                         </div>
                                     </div>
    
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Is This Your First Visit? / هل هذه الزيارة الأولى لنا?</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.IsFirstVisit, new { @class = "form-control" })*@
                                                 @Html.RadioButtonFor(model => model.IsFirstVisit, true) Yes
                                                 @Html.RadioButtonFor(model => model.IsFirstVisit, false) No
                                                 @*@Html.ValidationMessageFor(model => model.IsFirstVisit)*@
                                             </div>
                                         </div>
                                     </div>
                                 </div>
    
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-4">Checkin Date / تاريخ القدوم</label>
                                             <div class="col-md-8">
                                                 @Html.TextBoxFor(model => model.CheckinDate, new { @class = "form-control", @readonly = "readonly" })
                                                 @*@Html.ValidationMessageFor(model => model.CheckinDate, "", new { @class = "badge badge-danger" })*@
                                             </div>
                                         </div>
                                     </div>
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-4">Checkout Date / تاريخ المغادرة</label>
                                             <div class="col-md-8">
                                                 @Html.TextBoxFor(model => model.CheckOutDate, new { @class = "form-control", @readonly = "readonly" })
                                                 @*@Html.ValidationMessageFor(model => model.CheckOutDate, "", new { @class = "badge badge-danger" })*@
                                             </div>
                                         </div>
                                     </div>
                                 </div>
    
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-4">Mobile No / رقم الجوال</label>
                                             <div class="col-md-8">
                                                 @Html.TextBoxFor(model => model.MobileNumber, new { @class = "form-control", @readonly = "readonly" })
                                                 @*@Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "badge badge-danger" })*@
                                             </div>
                                         </div>
                                     </div>
    
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-4">Email ID / البريد الالكتروني</label>
                                             <div class="col-md-8">
                                                 @Html.TextBoxFor(model => model.EmailId, new { @class = "form-control" })
                                                 @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
    
                                 <hr />
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Cooperation of the receptionist / تعاون موظف الاستقبال</label>
                                             <div class="col-md-6">
                                                 @Html.DropDownListFor(model => model.OperationReceiption, new List<SelectListItem>{
                                                 new SelectListItem { Text = "----Select----", Value = "-1" },
                                                 new SelectListItem { Text = "Bad", Value = "1" },
                                                 new SelectListItem { Text = "Poor", Value = "2" },
                                                 new SelectListItem { Text = "Average", Value = "3" },
                                                 new SelectListItem { Text = "Good", Value = "4" },
                                                 new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.OperationReceiption, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Recreation areas in the reception lobby / مناطق الاستراحة في بهو الاستقبال</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.ReceiptionLoby)*@
    
                                                 @Html.DropDownListFor(model => model.ReceiptionLoby, new List<SelectListItem>{
                                                 new SelectListItem { Text = "----Select----", Value = "-1" },
                                                 new SelectListItem { Text = "Bad", Value = "1" },
                                                 new SelectListItem { Text = "Poor", Value = "2" },
                                                 new SelectListItem { Text = "Average", Value = "3" },
                                                 new SelectListItem { Text = "Good", Value = "4" },
                                                 new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.ReceiptionLoby, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Bag delivery service / خدمة توصيل الحقائب</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.BagDeliveryService)*@
    
                                                 @Html.DropDownListFor(model => model.BagDeliveryService, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.BagDeliveryService, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Business men Center / مركز رجال الأعمال</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.BusinessCenter)*@
    
                                                 @Html.DropDownListFor(model => model.BusinessCenter, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.BusinessCenter, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">The coffee shop / الكوفي شوب</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.CoffeShop)*@
    
                                                 @Html.DropDownListFor(model => model.CoffeShop, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.CoffeShop, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Mosque floor / دور المسجد</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.Mosque)*@
    
                                                 @Html.DropDownListFor(model => model.Mosque, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.Mosque, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Quality of the restaurant / جودة المطعم</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.Restaurent)*@
    
                                                 @Html.DropDownListFor(model => model.Restaurent, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.Restaurent, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Varieties of food / تنوع الأصناف</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.QualityofFood)*@
    
                                                 @Html.DropDownListFor(model => model.QualityofFood, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.QualityofFood, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Furniture standard in the room / مستوى الأثاث في الغرفة</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.FurnitureinRoom)*@
    
                                                 @Html.DropDownListFor(model => model.FurnitureinRoom, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.FurnitureinRoom, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">level of cleanliness / مستوى النظافة</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.LevelofCleanless)*@
    
                                                 @Html.DropDownListFor(model => model.LevelofCleanless, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @Html.ValidationMessageFor(model => model.LevelofCleanless, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-md-6">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Room service level / مستوى خدمات الغرف</label>
                                             <div class="col-md-6">
                                                 @*@Html.TextBoxFor(model => model.RoomService)*@
    
                                                 @Html.DropDownListFor(model => model.RoomService, new List<SelectListItem>{
                                                                       new SelectListItem { Text = "----Select----", Value = "-1" },
                                                                        new SelectListItem { Text = "Bad", Value = "1" },
                                                                        new SelectListItem { Text = "Poor", Value = "2" },
                                                                        new SelectListItem { Text = "Average", Value = "3" },
                                                                        new SelectListItem { Text = "Good", Value = "4" },
                                                                            new SelectListItem { Text = "Excellent", Value = "5" }}, new { @class = "star-rating" })
                                                 @*<span asp-validation-for="RoomService" class="text-danger"></span>*@
                                                 @Html.ValidationMessageFor(model => model.RoomService, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <hr />
                                 <div class="row">
                                     <div class="col-md-12">
                                         <div class="form-group">
                                             <label class="control-label col-md-6">Have you encountered any problem during your stay with us? / هل واجهتكم أي مشكلة أثناء اقامتكم معنا</label>
                                             <div class="col-md-12">
                                                 @*@Html.TextBoxFor(model => model.FeedBack)*@
                                                 @Html.TextAreaFor(mode => Model.FeedBack, new { @class = "form-control" })
                                                 @Html.ValidationMessageFor(model => model.FeedBack, "", new { @class = "badge badge-danger" })
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                                 <div class="row">
                                     <div class="col-md-12 text-center">
                                         <div class="form-group">
                                             <input type="submit" id="btnSubmit" value="Submit / موافق" class="btn btn-success" />
                                         </div>
                                     </div>
                                 </div>
                             </fieldset>
                         </div>
                     </div>
                 </div>
             </div>
         </div>
         @*@Html.AntiForgeryToken()*@
    
     }
    
     @Scripts.Render("~/bundles/jqueryval")
 <script src="~/Scripts/jquery-3.4.1.js"></script>
     <script type="text/javascript">
         $("body").on("click", "#btnSubmit", function () {
    
             var ReservationNumber = $('#ReservationNumber');
             var RoomNumber = $('#RoomNumber');
             var UniqueCode = $('#UniqueCode');
    
             var GuestName = $("#GuestName");
             var MobileNumber = $("#MobileNumber");
             var EmailId = $("#EmailId");
             var IsFirstVisit = $("#IsFirstVisit");
             var CheckinDate = $("#CheckinDate");
             var CheckOutDate = $("#CheckOutDate");
             var OperationReceiption = $("#OperationReceiption");
             var ReceiptionLoby = $("#ReceiptionLoby");
             var BagDeliveryService = $("#BagDeliveryService");
             var BusinessCenter = $("#BusinessCenter");
             var CoffeShop = $("#CoffeShop");
             var Mosque = $("#Mosque");
             var Restaurent = $("#Restaurent");
             var QualityofFood = $("#QualityofFood");
             var FurnitureinRoom = $("#FurnitureinRoom");
             var LevelofCleanless = $("#LevelofCleanless");
             var RoomService = $("#RoomService");
             var FeedBack = $("#FeedBack");
             var rev = {};
    
             rev.ReservationNumber = ReservationNumber.val();
             rev.RoomNumber = RoomNumber.val();
             rev.UniqueCode = UniqueCode.val();
    
             rev.GuestName = GuestName.val();
             rev.MobileNumber = MobileNumber.val();
             rev.EmailId = EmailId.val();
             rev.IsFirstVisit = IsFirstVisit.val();
             rev.CheckinDate = CheckinDate.val();
             rev.CheckOutDate = CheckOutDate.val();
    
             rev.OperationReceiption = OperationReceiption.val();
             rev.ReceiptionLoby = ReceiptionLoby.val();
             rev.BagDeliveryService = BagDeliveryService.val();
             rev.BusinessCenter = BusinessCenter.val();
             rev.CoffeShop = CoffeShop.val();
             rev.Mosque = Mosque.val();
             rev.Restaurent = Restaurent.val();
             rev.QualityofFood = QualityofFood.val();
             rev.FurnitureinRoom = FurnitureinRoom.val();
             rev.LevelofCleanless = LevelofCleanless.val();
             rev.RoomService = RoomService.val();
    
             rev.FeedBack = FeedBack.val();
    
             $.ajax({
                 type: "POST",
                 /*url: "/api/AjaxAPI/InsertCustomer",*/
                 /*url: "https://210.255.200.204:802/review/SaveReview",*/
                 url: "https://localhost:44322/review/SaveReview",
                 data: JSON.stringify(rev),
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function (data) {
                     if (data) {                        
                         alert("Your Feedback Is Successfully Saved.");
                         window.location.replace("https://localhost:44322/Home/Thanks");
                     }
                     else {                        
                         alert("Invalid Form data!");
                     }
                 }
             });
         });
     </script>
    
     @*<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>*@
     <script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
     <script>
         $(document).ready(function () {
             /*alert("TEST");*/
    
             $.ajax({
                 type: "GET",
                 /*url: "https://localhost:44322/review/GetReviewMainData?ReservationNumber=45&RoomNumber=10-1055&UniqueCode=DAR01",*/
                 url: "/review/GetReviewMainData?ReservationNumber=45&RoomNumber=10-1055&UniqueCode=DAR01",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function (response) {
                     if (response != null) {
    
                         $('#ReservationNumber').val(response.ReservationNumber);
                         $('#RoomNumber').val(response.RoomNumber);
                         $('#UniqueCode').val(response.UniqueCode);
    
                         $('#GuestName').val(response.GuestName);
                         $('#MobileNumber').val(response.MobileNumber);
                         $('#EmailId').val(response.EmailId);
                         $('#CheckinDate').val(response.CheckinDate);
                         $('#CheckOutDate').val(response.CheckOutDate);
                     } else {
                         alert("Something went wrong");
                     }
                 },
                 failure: function (response) {
                     alert(response.responseText);
                 },
                 error: function (response) {
                     alert(response.responseText);
                 }
             })
         })
     </script>
    
     <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js" integrity="sha512-YBk7HhgDZvBxmtOfUdvX0z8IH2d10Hp3aEygaMNhtF8fSOvBZ16D/1bXZTJV6ndk/L/DlXxYStP8jrF77v2MIg==" crossorigin="anonymous"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/autoloader/prism-autoloader.min.js" integrity="sha512-zc7WDnCM3aom2EziyDIRAtQg1mVXLdILE09Bo+aE1xk0AM2c2cVLfSW9NrxE5tKTX44WBY0Z2HClZ05ur9vB6A==" crossorigin="anonymous"></script>
    
     <script src="~/Scripts/star-rating.js?ver=4.1.4"></script>
     <script type="text/javascript">
         var destroyed = false;
         var starratingPrebuilt = new StarRating('.star-rating-prebuilt', {
             prebuilt: true,
             maxStars: 5,
         });
         var starrating = new StarRating('.star-rating', {
             stars: function (el, item, index) {
                 el.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect class="gl-star-full" width="19" height="19" x="2.5" y="2.5" /><polygon fill="#FFF" points="12 5.375 13.646 10.417 19 10.417 14.665 13.556 16.313 18.625 11.995 15.476 7.688 18.583 9.333 13.542 5 10.417 10.354 10.417" /></svg>';
             },
         });
         var starratingOld = new StarRating('.star-rating-old');
         document.querySelector('.toggle-star-rating').addEventListener('click', function () {
             if (!destroyed) {
                 starrating.destroy();
                 starratingOld.destroy();
                 starratingPrebuilt.destroy()
                 destroyed = true;
             } else {
                 starrating.rebuild();
                 starratingOld.rebuild();
                 starratingPrebuilt.rebuild()
                 destroyed = false;
             }
         });
     </script>
    
    
 </body>
 </html>









dotnet-aspnet-mvcdotnet-aspnet-ajax
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

the standard MVC validation (unobtrusive validation) fires when a submit button is clicked. Your design uses AJAX and a button click handler which does not apply.

You'll need to add JavaScript code/library to validate the inputs. If we assume you plan to use the standard client MVC validation then the following line will validate the form.

 $("form").validate();
 if ($("form")..valid()) {...

You also need to add model validation to the Web API actions which is the most important location for input validation. You're AJAX application must detect validation errors returning from an action and update the UI accordingly.

I recommend reading the ASP.NET MVC validation documentation which cover these concepts and also unobtrusive validation which illustrates how to write the client side code. Also, see the official Web API model validation docs which illustrate standard validation patterns.

Lastly, please remove the dotnet-aspnet-core-razor tag as your code is ASP.NET not Core.


0 Votes 0 ·

Can You Please Share Some Examples Of Simple Application Of Validation Which Uses AJAX POST.

thanks.

0 Votes 0 ·

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @MansoorAhmed-0534 ,

According to your codes,when you run the post method using the javascript ajax, it will return the data when successing. And then it will alert the successfully message.

If you want to validate the field,there are two ways:
First,you could validate in the model. Just like this:[Required].
Second you could add ModelState.IsValid in the controller.More details,you could refer to below article.




If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Yijing Sun


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.