6
votes

validation is working normal on local box, dev site but it is not happening on the staging and production sites. Both client side and server side validation is not happening. Both staging and production are load balanced but use sticky connection due to some other functional requirements.

I have checked the bin folder on all environments and i see the following two dlls there.

DataAnnotationsExtensions.ClientValidation.dll
DataAnnotationsExtensions.dll

On the server side, following should fail but it is not happening.

!TryValidateModel(model) || !ModelState.IsValid

This site use windows authentication.

Web.config

<appSettings file="Configs\AppSettings_LocalHost.config">
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>

For testing purposes, i am not using bundles at this time. For the bundles, i even tested it with following

<location path="~/Content">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
    <location path="~/bundles">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
    <location path="~/Scripts">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>

And i have the following JS files referenced as well

<script src="/NetSite/Scripts/Core/jquery.validate.min.js?v=1.12" type="text/javascript"></script>
<script src="/NetSite/Scripts/Core/jquery.validate.unobtrusive.min.js?v=1.12" type="text/javascript"></script>
<script src="/NetSite/Scripts/Custom/Validators.js?v=1.12" type="text/javascript"></script>

The app is MVC 5 and all has been added via the NuGet package. I don't have the MVC installed on the server. I have another MVC 5 app on these servers and validation is happening just fine.

And here is the form tag, the second working app uses the same form tag.

using (Html.BeginForm(ActionNames.Index, ControllerNames.Rankings, new { Area = AreaNames.MemberToolsReports }, FormMethod.Post, new { id = "RankingsSearchForm" }))

On the old staging and production boxes, validation was working but then we had MVC 3 installed on it.

Update - Controller Code

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using Project.BusinessEntities;
        using Project.Common.Constants;
        using Project.MvcBase;
        using Project.Resources;
        using Project.ServiceInterfaces;
        using Project.ViewModels;
        using Project.ViewModels.MemberToolReports;
        using Microsoft.Practices.Unity;
        using Project.Helpers.Helpers;
        using Project.Helpers.IO;

        namespace Project.Site.Areas.MemberToolsReports.Controllers
        {
            public class RankingsController : BaseController
            {
                #region PROPERTIES

                [Dependency]
                public IGeographyService GeographyServiceInstance { get; set; }

                [Dependency]
                public IRankingsService RankingsServiceInstance { get; set; }

                [Dependency]
                public IUtilityService UtilityServiceInstance { get; set; }

                #endregion

                #region ACTIONS

                public ActionResult Index()
                {
                    var states = getting states here
                    var key = String.Empty;

                    var search = new RankingSearch { Key = key };

                    var model = new RankingSearchViewModel { Search = search, StatesList = states };

                    return View(model);
                }

                [HttpPost]
                [ValidateAntiForgeryToken]
                public ActionResult Index(RankingSearchViewModel model)
                {
                    var errorModel = new ContentShowError { IsError = true };
                    var resultModel = new RankingsSearchResultsViewModel();

                    try
                    {   
                    //TODO: remove extra code once data annotations issue is fixed on staging and prod
                        if (!Request.IsAjaxRequest())
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorServicingRequest);
                        }
                        else if (!TryValidateModel(model) || !ModelState.IsValid)
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorProcessingRequest);
                        }
                        else if (String.IsNullOrWhiteSpace(model.Search.Key) &&
                                 String.IsNullOrWhiteSpace(model.Search.Institution) &&
                                 String.IsNullOrWhiteSpace(model.Search.State))
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.NoCriteriaSpecified);
                        }
                        else
                        {
                            //default - debug code
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorNoDataFound);

                            var results = RankingsServiceInstance.SearchRanking(model.Search);
                            if (results != null && results.Count > 0)
                            {
                                errorModel.IsError = false;
                                errorModel.Message = String.Empty;

                                //update result model
                                resultModel.Rankings = results;

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorProcessingRequest);
                        base.LogException(ex);
                    }
                    ActionResult result = null;
                    result = errorModel.IsError ? PartialView(ViewNames.ErrorControl, errorModel) : PartialView(ViewNames.SearchResultsControl, resultModel);

                    return result;
                }

                #endregion

            }
        }

Update 2 - HTML Difference

Looks like validation attributes are not even making it to the html, as if the site doesn't even know that we are using validation. Right now, both, dev and staging sites have the same code.

Staging site

<input autofocus="autofocus" class="clearSearchFields" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />

Working dev site

<input autofocus="autofocus" class="clearSearchFields" data-val="true" data-val-length="Key must be 6 characters long" data-val-length-max="6" data-val-length-min="6" data-val-regex="Only alphanumeric (A-Z a-z 0-9) values are allowed" data-val-regex-pattern="[A-Za-z0-9]*" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />
                                <span class="field-validation-valid" data-valmsg-for="Search.Key" data-valmsg-replace="true"></span>
2
can u show the controller - Sachu
any errors in console? And another silly question - have you add jquery to your page? - teo van kot
I have updated the case with controller code. No error in console showing. I have added jquery, local dev and dev site both are working. - learning...
Do you see data-attributes in html elements? - Amirhossein Mehrvarzi
@AmirHosseinMehrvarzi Yes on dev and no on staging... Update 2 shows html for both dev and staging. Currenlty both dev and staging sites have the same code base. - learning...

2 Answers

0
votes

Since you have had another mvc 5 app on that machine that which runs fine and you don't have MVC installed it would appear that something isn't being deployed correctly. Most likely something there is some assembly needed in the MVC package that you don't have.

Is there a reason you can't install MVC on the server? There are stand alone packages available for that. That should add everything to the GAC that you need.

If you can't install MVC I would look at the bin of your working MVC 5 app. Does it appears to have more .Net assemblies than your new application? If so then someone probably included all the missing MVC assemblies in it. You could try copying all the assemblies over from the working mvc app, just make sure you don't override. That should show you any assembly that you are missing.

0
votes

Since some of the posters do not read the fully question and comments and try to answer, i am moving my last two comments from the question thread as an answer. My issue is fixed.

I moved the web.config from my local machine over to staging and prod and validation started working. I have checked the old web.config and this new working folder web.config and there are no differences. Even though it is working, i am happy but i am now confused at the same time.

Looks like the ASP.NET temp file was an issue in this case. When i updated the web.config manually, the temp file got updated as well, which fixed the issue for me.