Modify DropdownList MVC using users?












0















It turns out that I have a Jquery from a dropdownlist in casacade, this contains data from districts and services and I want that when a user logs in, only shows the data of the district where the user is from, the user has an assigned district.



This is my code on the controller:



public JsonResult GetServices(int districtId)
{
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services.Where(s => s.DistrictId == districtId).OrderBy(s => s.Name);
return Json(services);
}


This is my Jquery Script:



<script type="text/javascript">
$(document).ready(function () {
$("#DistrictId").change(function () {
$("#ServiceId").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetServices")',
dataType: 'json',
data: { districtId: $("#DistrictId").val() },
success: function (districts) {
$.each(districts, function (i, service) {
$("#ServiceId").append('<option value="'
+ service.ServiceId + '">'
+ service.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve services.' + ex);
}
});
return false;
})
});
</script>


My View:



<div class="form-group">
@Html.LabelFor(model => model.DistrictId, "District", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DistrictId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DistrictId, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ServiceId, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ServiceId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ServiceId, "", new { @class = "text-danger" })
</div>
</div>









share|improve this question

























  • In the controller, you will need to get a reference to the current user (userId) and pass that value in the linq query to also filter by userId

    – Sergiu Muresan
    Nov 21 '18 at 17:42











  • Add a Where condition to your LINQ query to get data only for the userId. Without knowing your data model schema, It is hard to give you a specific answer.

    – Shyju
    Nov 21 '18 at 17:44













  • I would suggest clarifying what your question/intent is. The information you have provided is good, maybe add a little more info about your data model schema as @Shyju said.

    – HeedfulCrayon
    Nov 21 '18 at 17:50











  • Unrelated note about the visual-studio. If you read the description, it says DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio., so it should not be used in this question.

    – Richardissimo
    Nov 21 '18 at 20:51











  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:44
















0















It turns out that I have a Jquery from a dropdownlist in casacade, this contains data from districts and services and I want that when a user logs in, only shows the data of the district where the user is from, the user has an assigned district.



This is my code on the controller:



public JsonResult GetServices(int districtId)
{
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services.Where(s => s.DistrictId == districtId).OrderBy(s => s.Name);
return Json(services);
}


This is my Jquery Script:



<script type="text/javascript">
$(document).ready(function () {
$("#DistrictId").change(function () {
$("#ServiceId").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetServices")',
dataType: 'json',
data: { districtId: $("#DistrictId").val() },
success: function (districts) {
$.each(districts, function (i, service) {
$("#ServiceId").append('<option value="'
+ service.ServiceId + '">'
+ service.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve services.' + ex);
}
});
return false;
})
});
</script>


My View:



<div class="form-group">
@Html.LabelFor(model => model.DistrictId, "District", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DistrictId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DistrictId, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ServiceId, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ServiceId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ServiceId, "", new { @class = "text-danger" })
</div>
</div>









share|improve this question

























  • In the controller, you will need to get a reference to the current user (userId) and pass that value in the linq query to also filter by userId

    – Sergiu Muresan
    Nov 21 '18 at 17:42











  • Add a Where condition to your LINQ query to get data only for the userId. Without knowing your data model schema, It is hard to give you a specific answer.

    – Shyju
    Nov 21 '18 at 17:44













  • I would suggest clarifying what your question/intent is. The information you have provided is good, maybe add a little more info about your data model schema as @Shyju said.

    – HeedfulCrayon
    Nov 21 '18 at 17:50











  • Unrelated note about the visual-studio. If you read the description, it says DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio., so it should not be used in this question.

    – Richardissimo
    Nov 21 '18 at 20:51











  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:44














0












0








0








It turns out that I have a Jquery from a dropdownlist in casacade, this contains data from districts and services and I want that when a user logs in, only shows the data of the district where the user is from, the user has an assigned district.



This is my code on the controller:



public JsonResult GetServices(int districtId)
{
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services.Where(s => s.DistrictId == districtId).OrderBy(s => s.Name);
return Json(services);
}


This is my Jquery Script:



<script type="text/javascript">
$(document).ready(function () {
$("#DistrictId").change(function () {
$("#ServiceId").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetServices")',
dataType: 'json',
data: { districtId: $("#DistrictId").val() },
success: function (districts) {
$.each(districts, function (i, service) {
$("#ServiceId").append('<option value="'
+ service.ServiceId + '">'
+ service.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve services.' + ex);
}
});
return false;
})
});
</script>


My View:



<div class="form-group">
@Html.LabelFor(model => model.DistrictId, "District", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DistrictId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DistrictId, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ServiceId, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ServiceId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ServiceId, "", new { @class = "text-danger" })
</div>
</div>









share|improve this question
















It turns out that I have a Jquery from a dropdownlist in casacade, this contains data from districts and services and I want that when a user logs in, only shows the data of the district where the user is from, the user has an assigned district.



This is my code on the controller:



public JsonResult GetServices(int districtId)
{
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services.Where(s => s.DistrictId == districtId).OrderBy(s => s.Name);
return Json(services);
}


This is my Jquery Script:



<script type="text/javascript">
$(document).ready(function () {
$("#DistrictId").change(function () {
$("#ServiceId").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetServices")',
dataType: 'json',
data: { districtId: $("#DistrictId").val() },
success: function (districts) {
$.each(districts, function (i, service) {
$("#ServiceId").append('<option value="'
+ service.ServiceId + '">'
+ service.Name + '</option>');
});
},
error: function (ex) {
alert('Failed to retrieve services.' + ex);
}
});
return false;
})
});
</script>


My View:



<div class="form-group">
@Html.LabelFor(model => model.DistrictId, "District", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DistrictId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DistrictId, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ServiceId, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ServiceId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ServiceId, "", new { @class = "text-danger" })
</div>
</div>






c# jquery asp.net-mvc visual-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:43







SELSO ALONSO

















asked Nov 21 '18 at 17:39









SELSO ALONSOSELSO ALONSO

11




11













  • In the controller, you will need to get a reference to the current user (userId) and pass that value in the linq query to also filter by userId

    – Sergiu Muresan
    Nov 21 '18 at 17:42











  • Add a Where condition to your LINQ query to get data only for the userId. Without knowing your data model schema, It is hard to give you a specific answer.

    – Shyju
    Nov 21 '18 at 17:44













  • I would suggest clarifying what your question/intent is. The information you have provided is good, maybe add a little more info about your data model schema as @Shyju said.

    – HeedfulCrayon
    Nov 21 '18 at 17:50











  • Unrelated note about the visual-studio. If you read the description, it says DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio., so it should not be used in this question.

    – Richardissimo
    Nov 21 '18 at 20:51











  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:44



















  • In the controller, you will need to get a reference to the current user (userId) and pass that value in the linq query to also filter by userId

    – Sergiu Muresan
    Nov 21 '18 at 17:42











  • Add a Where condition to your LINQ query to get data only for the userId. Without knowing your data model schema, It is hard to give you a specific answer.

    – Shyju
    Nov 21 '18 at 17:44













  • I would suggest clarifying what your question/intent is. The information you have provided is good, maybe add a little more info about your data model schema as @Shyju said.

    – HeedfulCrayon
    Nov 21 '18 at 17:50











  • Unrelated note about the visual-studio. If you read the description, it says DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio., so it should not be used in this question.

    – Richardissimo
    Nov 21 '18 at 20:51











  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:44

















In the controller, you will need to get a reference to the current user (userId) and pass that value in the linq query to also filter by userId

– Sergiu Muresan
Nov 21 '18 at 17:42





In the controller, you will need to get a reference to the current user (userId) and pass that value in the linq query to also filter by userId

– Sergiu Muresan
Nov 21 '18 at 17:42













Add a Where condition to your LINQ query to get data only for the userId. Without knowing your data model schema, It is hard to give you a specific answer.

– Shyju
Nov 21 '18 at 17:44







Add a Where condition to your LINQ query to get data only for the userId. Without knowing your data model schema, It is hard to give you a specific answer.

– Shyju
Nov 21 '18 at 17:44















I would suggest clarifying what your question/intent is. The information you have provided is good, maybe add a little more info about your data model schema as @Shyju said.

– HeedfulCrayon
Nov 21 '18 at 17:50





I would suggest clarifying what your question/intent is. The information you have provided is good, maybe add a little more info about your data model schema as @Shyju said.

– HeedfulCrayon
Nov 21 '18 at 17:50













Unrelated note about the visual-studio. If you read the description, it says DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio., so it should not be used in this question.

– Richardissimo
Nov 21 '18 at 20:51





Unrelated note about the visual-studio. If you read the description, it says DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio., so it should not be used in this question.

– Richardissimo
Nov 21 '18 at 20:51













Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

– SELSO ALONSO
Nov 22 '18 at 0:44





Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

– SELSO ALONSO
Nov 22 '18 at 0:44












1 Answer
1






active

oldest

votes


















0














Assuming the logged user is consuming the controller method. You can obtain the user and filter by his district id without sending it as a parameter. If you have both dropdowns in the same View, what you are doing seems to be OK. If the problem is that it never goes inside the method, be sure you are using the corresponding Http Verb in this case POST.



[HttpPost]
public JsonResult GetUserServices()
{
var user = userManager.FindByNameAsync(User.Identity.Name);
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services
.Where(s => s.DistrictId == user.DistrictId)
.OrderBy(s => s.Name);
return Json(services);
}





share|improve this answer


























  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:42











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417763%2fmodify-dropdownlist-mvc-using-users%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Assuming the logged user is consuming the controller method. You can obtain the user and filter by his district id without sending it as a parameter. If you have both dropdowns in the same View, what you are doing seems to be OK. If the problem is that it never goes inside the method, be sure you are using the corresponding Http Verb in this case POST.



[HttpPost]
public JsonResult GetUserServices()
{
var user = userManager.FindByNameAsync(User.Identity.Name);
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services
.Where(s => s.DistrictId == user.DistrictId)
.OrderBy(s => s.Name);
return Json(services);
}





share|improve this answer


























  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:42
















0














Assuming the logged user is consuming the controller method. You can obtain the user and filter by his district id without sending it as a parameter. If you have both dropdowns in the same View, what you are doing seems to be OK. If the problem is that it never goes inside the method, be sure you are using the corresponding Http Verb in this case POST.



[HttpPost]
public JsonResult GetUserServices()
{
var user = userManager.FindByNameAsync(User.Identity.Name);
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services
.Where(s => s.DistrictId == user.DistrictId)
.OrderBy(s => s.Name);
return Json(services);
}





share|improve this answer


























  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:42














0












0








0







Assuming the logged user is consuming the controller method. You can obtain the user and filter by his district id without sending it as a parameter. If you have both dropdowns in the same View, what you are doing seems to be OK. If the problem is that it never goes inside the method, be sure you are using the corresponding Http Verb in this case POST.



[HttpPost]
public JsonResult GetUserServices()
{
var user = userManager.FindByNameAsync(User.Identity.Name);
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services
.Where(s => s.DistrictId == user.DistrictId)
.OrderBy(s => s.Name);
return Json(services);
}





share|improve this answer















Assuming the logged user is consuming the controller method. You can obtain the user and filter by his district id without sending it as a parameter. If you have both dropdowns in the same View, what you are doing seems to be OK. If the problem is that it never goes inside the method, be sure you are using the corresponding Http Verb in this case POST.



[HttpPost]
public JsonResult GetUserServices()
{
var user = userManager.FindByNameAsync(User.Identity.Name);
db.Configuration.ProxyCreationEnabled = false;
var services = db.Services
.Where(s => s.DistrictId == user.DistrictId)
.OrderBy(s => s.Name);
return Json(services);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 23:21









kamalpreet

1,2101435




1,2101435










answered Nov 21 '18 at 18:11









Charly GuiraoCharly Guirao

21123




21123













  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:42



















  • Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

    – SELSO ALONSO
    Nov 22 '18 at 0:42

















Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

– SELSO ALONSO
Nov 22 '18 at 0:42





Thanks I have these models users, districts and services, I have another model called fevers where users enter number of patients and number of patients with fever, the latter model requires district information and service. In the user model, it requires name, surname, user name, which is the email address and also requires which district and service they belong to. With all this, what I want to do is that when the user logs in, only the services of the user's district are loaded.

– SELSO ALONSO
Nov 22 '18 at 0:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417763%2fmodify-dropdownlist-mvc-using-users%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?