Feedback Submitting Without Validation

Mansoor Ahmed 1 Reputation point
2021-09-08T08:11:04.473+00:00

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 = "
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,284 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-09-13T07:45:13.737+00:00

    Hi @Mansoor Ahmed ,

    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

    0 comments No comments