I have a function in jQuery to disable input based on user roles. But I don't know how to get the current ASP.Net MVC user role in jQuery.
Below is the code but it is not working:
$(function () {
if (Roles.IsUserInRole('user'))
{
$("#GenericName").prop("disabled", true);
$("#TradeName").prop("disabled", true);
$("#Form").prop("disabled", true);
$("#Strength").prop("disabled", true);
$("#IsBrandSubstitutionAllowed").prop("disabled", true);
$("#Route").prop("disabled", true);
$("#Dosages").prop("disabled", true);
$("#Unit").prop("disabled", true);
$("#PackTypes").prop("disabled", true);
$("#GeneratedDirection").prop("disabled", true);
$("#UserDirection").prop("disabled", true);
$("#StartDate").prop("disabled", true);
$("#EndDate").prop("disabled", true);
}
});
GET is used to request data from a specified resource. With all the GET request we pass the URL which is compulsory, however it can take the following overloads. .get ( url [, data ] [, success (data, textStatus, jqXHR) ] [, dataType ] ).done/.fail Now, let's try to use GET in MVC application.
After you complete this tutorial, you will understand how controllers are used to control the way a visitor interacts with an ASP.NET MVC website. MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller.
Developers can build dynamic web applications using ASP.NET MVC framework that enables a clean separation of concerns, fast development, and TDD friendly. These tutorials are designed for beginners and professionals who want to learn ASP.NET MVC 5.
This is the first tutorial of a series that teaches ASP.NET Core MVC web development with controllers and views. At the end of the series, you'll have an app that manages and displays movie data. You learn how to: Add and scaffold a model. Work with a database. Add search and validation. View or download sample code ( how to download ).
The problem: You are mixing Javascript and Razor Views.
What you have:
if (Roles.IsUserInRole('user'))
{
should actually be written as:
var userRole = '@(Roles.IsUserInRole("user") ? "true" : "false")';
if(userRole) {
...
This code @(Roles.IsUserInRole('user') ? "true" : "false")
will output true
or false
literal string (as it's not wrapped in single or double quotes, javascript will interpreter as a boolean value) and you can just use that new assign variable.
What I normally do, is in my _Layout.cshtml
view, I add a simple Global Javascript that I can easily call through my application... for example and assuming that you have a CurrentUser
object on that View (through ViewData or Model):
<html>
<head>
<title>Your App</title>
<styles ... >
<script>
var AppGlobal = {
"user" = {
"name" : "@(CurrentUser.Name)",
"id" : "@(CurrentUser.Guid.ToString())",
"role" : "@(CurrentUser.Role.Name)"
},
...
};
</script>
</head>
<body>
@RenderBody()
</body>
</html>
Then it's easier, in your case to do:
if(AppGlobal.user.role === 'user') {
...
I would suggest to do an AJAX call in order to get user role.
After ajax calling, based on response received then you disable/enable inputs.
like:
$.ajax({
url: 'Controller1/Action1',
dataType: 'json',
data: {},
type: 'post',
success: function(data){
if(data.user === 'user') {
// disable inputs here !
}
}
});
and your controller called Controller1Controller
:
[HttpPost]
public JsonResult Action1(){
return Json(new {
user = // put current user role here !
});
}
Also, I'd suggest to use ValidateAntiForgeryToken
in order to avoid CSRF attack but seems to be fine since there are no form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With