ng-model value becoming null while disabling the selected value in dynamic dropdown list - asp.net

I have created a process for creating a dynamic dropdown list group on button click with the given number of the dropdown lists in an input field. Then i collect a user list from the database and showing all in these doropdown lists. Now my job is that if one user is selected by any dropdown then that option must be disabled for the others. How can i do that?
I tried that by disabling the selected option from the list, But when i am disabling the selected option, the dropdown ng-model that selected that option is becoming null.
For creating a dynamic Dropdown list:
<div class="col-md-3">
<div class="form-group-sm">
<input class="form-control" ng-model="numberOfLevels"
type="text" name="numberOfLevels" id="numberOfLevels">
</div>
</div>
<div class="col-md-7" >
<div class="form-group-sm">
<a href="#" type="button" class="btn btn-box-tool btn-primary"
style="color:white;"
ng-click="addLevels(numberOfLevels)" >Manage</a>
</div>
</div>
$scope.addLevels = function (num) {
getAllUsers();
$scope.showLevels = true;
$scope.levels = [];
var count = 0;
if (num > 0) {
while (count < num) {
count++;
//console.log(count);
$scope.levels.push({ 'key': count, 'value': count });
//console.log(count);
}
}
//console.log($scope.levels);
//getAllUsers();
$scope.approversInputList = [];
}
<div class="col-md-12" ng-show="showLevels" ng-repeat="a in levels">
<div class="form-group">
<div class="col-md-4">
<label for=" username">
Approver {{a.value}} :
</label>
<select class="browser-default custom-select form-control has-error"
ng-model="abc[a.value]"
ng-change="setApprovers(selectedFileId, selectedNoteSheetId,a.value, abc[a.value])">
<option></option>
<option id="select" ng-repeat="user in userList"
value="{{user.Id}}" ng-disabled="user.Disabled">
{{user.UserName}}
</option>
#*<option id="select" ng-repeat="user in userList"
value="{{user.Id}}"
ng-disabled="disabledList[user.Id]">
{{user.UserName}}
</option>*#
</select>
</div>
<div class="col-md-7">
</div>
</div>
</div>
User List View Model:
public int Id { get; set; }
public string UserName { get; set; }
public bool Disabled { get; set; }
AngularJs Code for Disabling Selected Option:
$scope.abc = {};
$scope.$watch('abc', function (newVal, oldVal) {
//console.log($scope.userList);
var aaa = $scope.approversInputList;
for (var i = 0; i < $scope.userList.length; i++) {
$scope.userList[i].Disabled = false;
//console.log($scope.abc);
angular.forEach($scope.abc, function (value) {
if ($scope.userList[i].Id == value) {
$scope.userList[i].Disabled = true;
//$scope.disabledList[value] = true;
}
});
}
//console.log($scope.disabledList);
}, true);
When the selected option is disabled the dropdown should show the selected username.

This is happening because your userList is only one instance for all select. So you set disabled in userList and it propagates to all the levels.
u can try this, it will be difficult to solve it exactly until u give a minimal reproducible example:
<select class="browser-default custom-select form-control has-error"
ng-model="abc[a.value]"
ng-change="setApprovers(selectedFileId, selectedNoteSheetId,a.value, abc[a.value])">
<option></option>
<option id="select{{$index}}" ng-repeat="user in userList"
value="{{user.Id}}" ng-disabled="abc[a.value] == user.Id">
{{user.UserName}}
</option>
</select>
And remove your for loop altogether. if ng-disabled="abc[a.value] == user.Id" does not work then instead of using ng-diabled use some class if the condition is met and do some css on that class to make it disabled urself.

Related

How to pass ID to controller and use it to display data on modal

I have passed the UserID to the table as shown in the code:
View
#foreach (var user in Model.RIROUsers)
{
<tr>
<td class="text-center">#(user.FirstName + " " + user.LastName)</td>
<td>View Details</button>
</tr>
}
Modal
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="form-group col-md-4 col-md-offset-2">
<label for="">* Address</label>
<input type="text" class="form-control input-sm" value="">
<label for="">* Birthday</label>
<input type="text" class="form-control input-sm" value="">
<label for="">* Contact</label>
<input type="text" class="form-control input-sm" value="">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Controller
public ActionResult Index()
{
var vm = new RIROViewModel();
vm.RIROUsers = new List<ResourceViewModel>();
foreach (var user in db_RIRO.sp_GetAllRIRORoster())
{
vm.RIROUsers.Add(new ResourceViewModel()
{
EID = user.EID,
FirstName = user.FirstName,
LastName = user.LastName,
EmployeeType = user.EmployeeType,
ProjectName = user.ProjectName,
JobTitle = user.JobTitle,
Level = user.CareerLevel,
ACNRollOn = user.HireDate,
ManagerName = user.Manager,
LeadName = user.Supervisor,
UserID = user.UserID
});
}
return View(vm);
}
Now when I click View Details I want the details of the user to display on the modal. What would be the best approach on this? I'm thinking of creating another stored procedure to retrieve the User's details and display it on the modal. But how do I do this?
EDIT 1: Changed button to a href=~/Views/RIRO/id=#user.UserID
As Shyju said, use click DOM event to the anchor link to show modal popup. Here are the steps:
1) Put Url.Action with proper controller and action name into href attribute of anchor link, then assign a class name to help identify each link.
<a href="#Url.Action("Details", "User", new { id = user.UserID })"
class="btn btn-xs btn-primary details" data-toggle="modal"
data-target="#exampleModal" data-id="#user.UserID">View Details</a>
2) Handle click DOM event using JS which rather than opening a new window/tab in browser, it should open modal popup instead (by using jQuery.ajax(), jQuery.get() or jQuery.load()):
$('.details').click(function (ev) {
ev.preventDefault(); // this is required
// get target action
var action = $(this).attr('href');
$.get(action, function (data) {
$(this).find(".modal-body").html(data);
$('#exampleModal').modal('show');
});
});
// alternative way
$('.details').click(function (ev) {
ev.preventDefault(); // this is required
// get target action
var action = $(this).attr('href');
$('.modal-body').load(action, function () {
$('#exampleModal').modal('show');
});
});
3) Ensure that target action method returns partial view which will loaded to the modal popup.
public ActionResult Details(int id)
{
var vm = new RIROViewModel();
// do something to retrieve data from DB
return PartialView("_Details", vm);
}
References:
Implementing Bootstrap Modal Popup In MVC Application
How to get anchor text/href on click using jQuery?

Use drop down result to filter another dropdown C# MVC

I am trying to update a drop down based on the users selection in the first drop down. I found this SO post on the topic, but Im not quite sure how to add the jquery to filter values to my Razor view. Can someone point me in the right direction?
My view so far:
#using System.Security.Claims;
#model UpdateClaimFormModel;
<h2>UpdateClaimView</h2>
#using (#Html.BeginForm())
{
<select name="emailcheese">
<option selected disabled>Select User</option>
#foreach (var i in Model.UserNames)
{
<option>#i.ToString()</option>
}
</select>
<select name="authpolicy">
<option selected disabled>Select Claim:</option>
#foreach (var i in Model.UserClaims)
{
<option>#i.Type.ToString()</option>
}
</select>
<div class="form-group">
<button type="submit" class="btn btn-default">Whoo</button>
</div>
}
My Goal:
Select User [bob, bill, jim]
Select Claim: If user bob:[1,2,3], If user bill:[1,4,8], If user him:[4,5,6]
so simpley you can do something like this
#using (#Html.BeginForm())
{
<select name="emailcheese" id="selectUserNames">
<option selected disabled>Select User</option>
#foreach (var i in Model.UserNames)
{
<option>#i.ToString()</option>
}
</select>
<select name="authpolicy" id="selectAuthPolicy">
</select>
<div class="form-group">
<button type="submit" class="btn btn-default">Whoo</button>
</div>
}
now in the script part
$(function(){
var userClaims = #Html.Raw(Json.Encode(Model.UserClaims));
$(document).on('change','#selectUserNames'function(){
var selectedVal = $(this).val();
if(selectedVal == 'bob')
{
var html ='<option selected disabled>Select Claim:</option>';
for(var i=1;i<userClaims.length;++i)
{
if(i==1||i==2||i==3)
{
html=html+'<option>'+userClaims[i].Type+'</option>';
}
}
$('#selectAuthPolicy').html(html);
}
})
})
this is just a basic dea of how you can achieve this i just tried for bob there are many other ways to immplement ths

How can i get a dropdownbox working with a css class?

I have a SelectList in the controller that its value passed to the view by ViewData to the view as below:
List<string> available = new List<string>();
available.AddRange(product.AvailableSizes.Split(',').ToList());
ViewData["availablesize"] = new SelectList(available);
in the view i have a drop down box which display the values of the variable availablesize as below:
#Html.DropDownList("availablesize")
for some reasons i need to display this drop down box in the following format but unable to handle this issue any suggestion?
<div class="product-page-options">
<div class="pull-left">
<label class="control-label">Size:</label>
<select class="form-control input-sm">
<option>L</option>
<option>M</option>
<option>XL</option>
</select>
</div>
</div>
This code must work, you can give css attributes as well in the razor syntax.
Also Make sure your ViewData["availablesize"] has IEnumerable<SelectListItem>
Change your controller code to below
ViewData["availablesize"] = new SelectList((IEnumerable)available);
in Views syntax must be like below
<div class="product-page-options">
<div class="pull-left">
<label class="control-label">Size:</label>
#Html.DropDownList("availablesize", ViewData["availablesize"], new {#class = "form-control input-sm "})
</div>
</div>
This is the syntax that you require
If you want to style each option differently, depending on the values passed, you can use some JQuery for that:
$(document).ready(function() {
$("#availablesize option").each(function() {
if ($(this).val() != '') {
switch ($(this).val()) {
case 'S':
$(this).css('background-color', 'lightgreen');
break;
case 'M':
$(this).css('background-color', 'green');
break;
case 'L':
$(this).css('background-color', 'orange');
break;
case 'XL':
$(this).css('background-color', 'red');
break;
default:
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="availablesize">
<option value="">- select size -</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
Fiddle

Meteor IF statement should be firing

I'm stumped as to why part of my code isn't displaying when the session variable is set to true. When the user selects a certain option from the dropdown, based on the value, it changes the warmerselected to TRUE. I have tracked this in the console, and it works just fine.
Here is my code:
HTML:
<div class="item">
<div class="ui label">Product Type:</div>
<select id="ProductType" name="ProductType">
{{#each ProductTypes}}
<option value="{{ProductTypeAbbrev}}">{{ProductTypeName}}</option>
{{/each}}
</select>
</div>
{{#if warmerselected}}
<div class="item">
<div class="ui label">Name:</div>
<input type="text" name="ProductName" placeholder="Enter Product Name">
</div>
{{/if}}
JS:
Session.setDefault("warmerselected", false);
Template.addProduct.events({
'change #ProductType': function (e) {
var selitem = $("#ProductType").val();
if(selitem == "WA") {
Session.set("warmerselected",true)
}else {
Session.set("warmerselected",false);
}
}
});
Template.addProduct.helpers({
warmerselected: function(){
return Session.get("warmerselected");
}
});
Session isn't meant to be helper in HTML, you have to create one

Show existing value in dropdownlist

I have a webgrid that already show values from my database. When a user clicks to edit, I want one of the field Status to be displayed as a dropdownlist. Below is my code:
#{
var db = Database.Open("doctors");
var statusResults = db.Query("SELECT Distinct Status FROM cpd_certificates")
.Select(category => new SelectListItem {
Text = category.Status
});
}
//html code
<div class="row">
<span class="label"><label for="status"> Status:</label></span>
#Html.DropDownList("Status",null, statusResults )
</div>
My dropdownlist list appears but I want the existing value to show of which the user can now change.
Tried and tested: See this site for more tutorials http://www.mikesdotnetting.com/Article/176/WebMatrix-and-jQuery-Forms-Part-2-Editing-Data
#{
var db = Database.Open("doctors");
var stat = db.Query("SELECT Distinct Status FROM cpd_certificates");
}
<div class="row">
<select name="status" id="edit-status">
<option value="">-- Select Status --</option>
#{
foreach(var statusResult in stat) {
<option value="#statusResult.Status"
selected="selected">#statusResult.Status</option>
}
}
</select>
</div>

Resources