Base solution for your next web application
Open Closed

Abp.NameValue to fill HTML.DropDownList. using Entity. #8709


User avatar
0
AstarIT created

INTERFACE List<NameValue<string>> GetAllBusinessType(string searchTerm);

SERVICE private readonly IRepository<BusinessType> _businesstypeRepository;

	[AbpAuthorize(AppPermissions.Pages_Administration_TenantExtendeds)]
	public List&lt;NameValue&lt;string&gt;> GetAllBusinessType(string searchTerm)
	{

		var busineestype = _businesstypeRepository.GetAll().WhereIf(
				!searchTerm.IsNullOrWhiteSpace(),
				p => p.Name.ToLower().Contains(searchTerm.ToLower())
			)
			.OrderBy(p => p.Name);

		

		return busineestype.Select(p => new NameValue&lt;BusinessType&gt;(p.Name, p)).ToList();


	}

PAGE <div class="form-group"> <label for="TenantExtended_BusinessType">@L("Business Type")</label> @Html.DropDownList("businesstype", new SelectList(Model.TenantExtendedBusinessTypeList, "Id", "DisplayName", (Model.IsEditMode ? Model.TenantExtended.BusinessType.ToString() : "")), @L("Select Business Type"), new { @class = "form-control kt-select2" }) </div>

JQUERY $(".kt-select2").select2({ placeholder: 'Select', ajax: { url: abp.appPath + "api/services/app/TenantExtendeds/GetAllBusinessTypeForTableDropdown", dataType: 'json', delay: 250, data: function (params) { return { searchTerm: params.term, // search term page: params.page }; }, processResults: function (data, params) { params.page = params.page || 1;

            return {
                results: $.map(data.result, function (item) {
                    return {
                        text: item.name,
                        id: item.value
                    }
                }),
                pagination: {
                    more: (params.page * 30) &lt; data.result.length
                }
            };
        },
        cache: true
    },
    minimumInputLength: 1,
    language: abp.localization.currentCulture.name
});

========= On the below line on SERVICE got the error, and I am following the same advise I .

return busineestype.Select(p => new NameValue<BusinessType>(p.Name, p)).ToList();

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<Abp.NameValue<OneServDemo.BusinessTypes.BusinessType>>' to 'System.Collections.Generic.List<Abp.NameValue<string>>' OneServDemo.Application E:\Multi Tenant Demos\OneNetZero Demos\OneServ DropDownBox\cesarzam26\OneServDemo\src\OneServDemo.Application\TenantExtendeds\TenantExtendedsAppService.cs 257 Active

Can you please help me out on this I have work all day on this and I can get it resolved. All help it is really appreciated. Thank you


9 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @AstarIT

    Have you solved this problem ?

    Thanks,

  • User Avatar
    0
    AstarIT created

    Nope, I still stuck on it. The convertion des not work.

    Error Connot Implicity Convert type, System.Collection.Generic.List<Abp.NameValue<OneServDemo.BusinessTypes.BusinessType>> to System.Collections.Generic.List<Abp.NameValue<srting>> This is the Method

    public List&lt;NameValue&lt;string&gt;> GetAllBusinessType2(string searchTerm)
    	{
    
    		var businesstype = _businesstypeRepository
    			.GetAll()
    			.WhereIf(
    				!searchTerm.IsNullOrWhiteSpace(),
    				p => p.Name.ToLower().Contains(searchTerm.ToLower())
    			).OrderBy(p => p.Name);
    
    		return businesstype.Select(p => new NameValue&lt;BusinessType&gt;(p.Name, p)).ToList();
    	}
    

    Now, since this is not working I use a different aproach

    public List<KeyValuePair<string, string>> GetAllBusinessType(string searchTerm) { var ListOFBusinessType = _businesstypeRepository.GetAll().ToList(); var list = new List<KeyValuePair<string, string>>();

    		foreach (var item in ListOFBusinessType)
    		{
    			list.Add(new KeyValuePair&lt;string, string&gt;(item.Name, item.Id.ToString()));
    		};
    
    		//return list.Where(c => c.Key.ToLower().Contains(searchTerm.ToLower())).ToList();
    		var xList= list.Where(c => c.Key.ToLower().Contains(searchTerm.ToLower())).ToList();
    		return list.Where(c => c.Key.ToLower().Contains(searchTerm.ToLower())).ToList();
    	}
    

    This generate the data very good but, it does not present the data in the front end. on the mitronics controls. The Single Select and multiple select des not present data. it does the function on search and I go data but it dees not present data in the controls data in controls are empty.

  • User Avatar
    0
    AstarIT created
    	public List&lt;KeyValuePair&lt;string, string&gt;> GetAllBusinessType(string searchTerm)
    	{
    
    		// Do by Cesar
    		var ListOFBusinessType = _businesstypeRepository.GetAll().ToList();
    		var list = new List&lt;KeyValuePair&lt;string, string&gt;>();
    
    		foreach (var item in ListOFBusinessType)
    		{
    			list.Add(new KeyValuePair&lt;string, string&gt;(item.Name, item.Id.ToString()));
    		};
    
    		//return list.Where(c => c.Key.ToLower().Contains(searchTerm.ToLower())).ToList();
    		var xList= list.Where(c => c.Key.ToLower().Contains(searchTerm.ToLower())).ToList();
    		return list.Where(c => c.Key.ToLower().Contains(searchTerm.ToLower())).ToList();
    	}
    

    Sorry the code got wrong. this is the approach that I am taking

  • User Avatar
    0
    AstarIT created

    In the approach that you suggesting return businesstype.Select(p => new NameValue<BusinessType>(p.Name, p)).ToList();

    Why do I have to write here my "BusinessType" Entity here ?

  • User Avatar
    0
    AstarIT created

    This are the values that I got from debuging data using my approach {result: Array(3), targetUrl: null, success: true, error: null, unAuthorizedRequest: false, …} result: Array(3) 0: {key: "Contractor", value: "2"} 1: {key: "Sub-Contractor", value: "3"} 2: {key: "Exclusive Member", value: "5"} length: 3__proto__: Array(0)targetUrl: nullsuccess: trueerror: nullunAuthorizedRequest: false__abp: true__proto__: Object

    result: Array(2), targetUrl: null, success: true, error: null, unAuthorizedRequest: false, …} result: Array(2) 0: {key: "Sub-Contractor", value: "3"} 1: {key: "Exclusive Member", value: "5"} length: 2__proto__: Array(0)targetUrl: nullsuccess: trueerror: nullunAuthorizedRequest: false__abp: true__proto__: Object

  • User Avatar
    0
    AstarIT created

    $(".kt-select2").select2({ placeholder: 'Select', ajax: { url: abp.appPath + "api/services/app/TenantExtendeds/GetAllBusinessType", dataType: 'json', delay: 250, data: function (params) { return { searchTerm: params.term, // search term page: params.page }; },

            processResults: function (data, params) {
                params.page = params.page || 1;
                console.log(data);
                return {
                    results: $.map(data.result, function (item) {
                        return {
                            text: item.name,
                            id: item.value
                           
                        }
                        
                    }),
                    pagination: {
                        more: (params.page * 30) < data.result.length
                    }
                    
                };
                
            },
            cache: true
           
        },
        minimumInputLength: 1,
        language: abp.localization.currentCulture.name
        
        
    });
    

    That is Metronic Function on Single Selcect and Multiple Select, Using Select2. As you can see in my results data is passed. bu does not show the list of selected data in Metronics controls.

  • User Avatar
    0
    AstarIT created

    This are the result form the samples of Metronics, Under DEMOUICOMPONENTS

    {result: Array(12), targetUrl: null, success: true, error: null, unAuthorizedRequest: false, …} result: Array(12) 0: {name: "United States of America", value: "2"} 1: {name: "Russian Federation", value: "3"} 2: {name: "France", value: "4"} 3: {name: "Spain", value: "5"} 4: {name: "Germany", value: "6"} 5: {name: "Netherlands", value: "7"} 6: {name: "China", value: "8"} 7: {name: "Italy", value: "9"} 8: {name: "Switzerland", value: "10"} 9: {name: "South Africa", value: "11"} 10: {name: "Brazil", value: "13"} 11: {name: "India", value: "14"} length: 12 proto: Array(0) targetUrl: null success: true error: null unAuthorizedRequest: false __abp: true proto: Object

    THIS IS WHAT I GOT USING MY METHOD 0: {key: "Contractor", value: "2"} 1: {key: "Sub-Contractor", value: "3"} 2: {key: "Exclusive Member", value: "5"}

    THIS IS METRONICS 0: {name: "United States of America", value: "2"} 1: {name: "Russian Federation", value: "3"} 2: {name: "France", value: "4"} 3: {name: "Spain", value: "5"}

    As we can see the only difference here is the Key... one is called key and the other one is called name.

  • User Avatar
    0
    AstarIT created

    I got the solution!!!! Solution:

    	public List&lt;NameValue&lt;string&gt;> GetAllBusinessType3(string searchTerm)
    	{
    		var businesstype = _businesstypeRepository.GetAll().ToList();
    		var businesstypelist = new List&lt;NameValue&lt;string&gt;>();
    		foreach (var item in businesstype)
    		{
    			businesstypelist.Add(new NameValue { Name = item.Name, Value = item.Id.ToString() });
    
    		}
    
    		return businesstypelist.Where(c => c.Name.ToLower().Contains(searchTerm.ToLower())).ToList();
    
    	}
    

    Works like a charm.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks a lot @AstarIT :)