I use remote attribute for chek "ExistUserName". I need to send old and new username to method for if equal to old username return false and if just equal to new username return true. How can I set the parameter for method on the page?
[Remote(action: "ExistUserNameForEdit",
controller: "User",
HttpMethod = "POST",
AdditionalFields = "__RequestVerificationToken , old")]
public string uUserName { get; set; }
It is better to say that I need to check with the method that if the string userName enters the current string userName , the method will not give a negative message, and if the entered string userName is different from the current userName , it will give a negative message. Because now the current userName method gives the user an error message if I do not want to give
CodePudding user response:
MVC supports unobtrusive client-side validation that is, some attributes are automatically added to the HTML element generated by the view engine. These attributes are then parsed and interpreted by the javascript library (jQuery) built in the MVC framework, performing the actual validation.
When you use the [Remote]
validation attribute you're telling the framework you want to use the controller action method you provided in the attribute constructor to check validation of the property you have decorated with that attribute. This means that you don't have to set up the arguments for your validation method: the parameter will have the same name as the property being validated.
The attribute you've added to your property will be automatically translated in an Ajax request to the following method in the User
controller:
[HttpPost]
public JsonResult ExistUserNameForEdit(string uUsername,
string __RequestVerificationToken,
string old)
To make all this works, be sure to add reference to the jquery-validation
and jquery-validation-unobtrusive
packages in your project