ASP.NET MVC3: Why remote validation with RemoteAttribute works only on client side?

That's how the Remote validation is designed. It sends an AJAX request to a controller action in order to perform some validations and if those validations fail, prevent the from submitting.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I am trying to implement remote validation with the new RemoteAttribute, as describe in the following MSDN article How to: Implement Remote Validation in ASP. NET MVC. Client side validation works fine, but when I submit, validation does not work anymore and data is stored into db... I must necessary implement a server side validation too, or I am not doing all the necessary to prevent the submit?

Asp. Net-mvc-3 validation server remote link|improve this question edited Jan 12 at 8:44N.N.1,6262621 asked Jan 12 at 6:51Larry8118 61% accept rate.

That's how the Remote validation is designed. It sends an AJAX request to a controller action in order to perform some validations and if those validations fail, prevent the from submitting. You are correct into thinking that you should perform the same validation once the form is submitted because the user can disable javascript and be able to submit without any client validation being performed.

So for example you could externalize this validation logic into a separate method which you would call from the controller action used for remote validation and from your POST action once the form is submitted. Here are significant parts of my code: MODEL Remote("CheckEsistenzaTicket", "Validation") public string TicketHD { get; set; } VALIDATION CONTROLLER OutputCache(Location = OutputCacheLocation. None, NoStore = true) public class ValidationController : Controller { private ITManagerRepository tmanagerRepository; public ValidationController() { this.

TmanagerRepository = new TManagerRepository(new TManagerContext()); } public JsonResult CheckEsistenzaTicket(string TicketHD) { if (!tmanagerRepository. ChkTicketExists(TicketHD)) return Json(true, JsonRequestBehavior. AllowGet); string esiste = String.

Format(CultureInfo. InvariantCulture, "Il ticket {0} risulta già caricato! ", TicketHD); return Json(esiste, JsonRequestBehavior.

AllowGet); } } VIEW @Html. EditorFor(model => model. TicketHD) @Html.

ValidationMessageFor(model => model. TicketHD).

Thank you @Darin Dimitrov. I would implement a validation logic server side too. But just another question: how MVC3 'standard' validation works?

I set other attribute rules on my model, as Required, MaxLength, etc...why for these rules, when I submit, I get validation errors, while for my Remote validation I do not have the same behavior? Does exist a way to make such validation work for the Remote attribute too? Thank you!

– Larry Jan 12 at 12:40 @Larry, validation attributes all derive from ValidationAttribute. The default model binder invokes validation when it tries to bind the action arguments from the request values. So all the IsValid methods are invoked on data annotations including the RemoteAttribute.

Except that, here's how the IsValid method is implemented for this attribute: public override bool IsValid(object value) { return true; }. Now you understand why it does not validate anything on the server. If you want to perform the validation you could derive from it and override this method.

– Darin Dimitrov Jan 12 at 12:55 thanks again. I am new in programming. Could you better explain me how I can derive the method in my case?

I put some significant parts of my code inside original question, THX – Larry Jan 12 at 13:40 1 @Larry, you derive from RemoteAttribute and override the IsValid method. If this doesn't make sense to you, you should probably take some time to learn basic OOP and . NET before jumping into ASP.

NET MVC. – Darin Dimitrov Jan 12 at 13:42.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions