Friday, May 21, 2021

MVC checkbox list

 Although it appears to be rather simple taks to add a checkbox list for multiselect items in MVC, in reality it takes a bit of time to get around the concepts and limitations of the framework. So here is a step-by-step list to save the exploration time.

Disclaimer: This article uses techniques mentioned in the last answer at https://stackoverflow.com/questions/40160998/checkboxlist-using-asp-net-mvc-5

 

  1. I had two lists in the controller, one list of all possible values and another of the selected items. To be able to display all values from one list and pre-select only  those which were present in the second list, it is very convenient to use a CheckboxViewModel class as explained in the last answer of above post.
  2. I also added one more property to the CheckboxViewModel class which stores id of the object to which it belongs, thus this simplifies the process of generating entities for database update.
  3. I found out that IEnumerable<CheckboxViewModel> for checkbox binding in view fails to bind values properly in postback methods, so I changed the property type to CheckboxViewModel[].
  4. Need to add hidden fields for each of the property of  CheckboxViewModel class.
 
 
Thus CheckboxViewModel class becomes 
      

public class CheckboxViewModel

    {

        public int Id { get; set; }

 

        public string Name { get; set; }

 

        public bool Checked { get; set; }

 

        public int ParentId { get; set; }

    }

 

 And final extension method looks like

 

public static CheckboxViewModel[] CreateCheckboxList<T>(this IEnumerable<T> entities, Func<T, int> value, Func<T, object> text, Func<T, bool> isChecked, Func<T, int> parentId)

        {

            return entities.Select(x => new CheckboxViewModel

            {

                Id = (int)value(x),

                Name = text(x).ToString(),

                Checked = isChecked(x),

                ParentId = parentId(x)

            }).ToArray();

        }

 
   View looks like

<div class="form-group">

    <div class="checkbox checkbox-primary">

        @Html.HiddenFor(model => model.MyCheckboxList[i].Id)

        @Html.CheckBoxFor(model => model.MyCheckboxList[i].Checked)

        @Html.HiddenFor(model => model.MyCheckboxList[i].Checked)

        @Html.LabelFor(model => model.MyCheckboxList[i].Checked, Model.MyCheckboxList[i].Name)

        @Html.HiddenFor(model => model.MyCheckboxList[i].Name)

        @Html.HiddenFor(model => model.MyCheckboxList[i].ParentId)

    </div>

</div>

Wednesday, May 5, 2021

SharePoint Error: Access Denied. You do not have permission to perform this action or access this resource

 This error can occur at many places e.g. creating a workflow, accessing list items or user permissions using CSOM, or simply trying to navigate to the settings page. Its meaning is pretty straight forward. In dev server setup it can be resolved by providing full control permissions to the dev users.


  • Go to Central Administration
  • Go to Application Management
  • Go to Manage Web Application
  • Select the web application we’re talking about
  • Click User Policy
  • Add Users
  • Click Next
  • Fill in domain\superuser
  • Select Full Control
  • Click OK

For staging and production environments though, we should be careful to assing only the required permissions.


Reference: http://www.sharepointchick.com/archive/2010/10/06/resolving-the-super-user-account-utilized-by-the-cache-is.aspx

Thursday, April 1, 2021

WebApi 2 - CORS error after deployment Access to the path '/bin/roslyn/csc.exe' denied

 This error can be seen after redeployment of the API website in developer tools (F12) 

 

This is a misleading error message; real underlying problem is application pool account for the API website does not have execute permissions on the API website’s folder.  

The permissions problem can be seen by trying to open api website on the IIS Server hosting this website. If we try to send AJAX request to the API website on the hosting IIS server, the error message changes to 




[Win32Exception (0x80004005): Access is denied] 

 

[ExternalException (0x80004005): Cannot execute a program. The command being executed was "<path of the API website folder on server>/bin/csc.exe" /shared /keepalive:"10" /noconfig  /fullpaths @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\5a229d66\a33ece8d\xf2kzry1.cmdline".]



 


This problem can be fixed by navigating to the IIS server and providing full control permissions for app pool account on the API Website folder.



If we don't want this issue to repeat after every deployment, we can add IncludeSetAclProviderOnDestination tag in the publish profile (.pubxml) file as explained at https://stackoverflow.com/questions/54911986/iis-application-pool-identity-permissions-reset-on-every-visual-studio-app-publi



<?xml version="1.0" encoding="UTF-8"?> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <PropertyGroup> <IncludeSetAclProviderOnDestination>False</IncludeSetAclProviderOnDestination> </PropertyGroup> </Project>

This parameter can also be added to .csproj file or as msbuild parameter as exlpained at https://blogs.iis.net/msdeploy/skipping-setting-an-acl-in-a-visual-studio-2010-deployment-package


1) Edit the .csproj file and set  <IncludeSetAclProviderOnDestination>False</IncludeSetAclProviderOnDestination>

2) msbuild.exe myproject.csproj /p:IncludeSetAclProviderOnDestination=False


Another way of specifying permissions is explained at https://davidsonsousa.net/blog/post/setting-folder-permissions-using-web-deploy


More references/options:



  1. https://docs.microsoft.com/en-us/answers/questions/202683/why-is-access-to-the-path-39binroslyn39-denied.html
  2. https://stackoverflow.com/questions/54893505/server-error-in-application-access-is-denied
  3. https://stackoverflow.com/questions/32780315/could-not-find-a-part-of-the-path-bin-roslyn-csc-exe


Wednesday, March 3, 2021

Partial views in bootstrap modal using AJAX

 This article explains how partial views can be used in bootstrap modal for CRUD operations on a related child entity. It uses ajax to dynamically load and save content displayed in a bootstrap modal dialog box.


https://www.codeproject.com/Articles/786085/ASP-NET-MVC-List-Editor-with-Bootstrap-Modals

Tuesday, February 16, 2021

Regex Email validation in c# dot net core

 Use this regex /^_?[a-zA-Z0-9]([a-zA-Z0-9]*[._+-])*[a-zA-Z0-9_]+@(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.(?!-)[A-Za-z0-9-]{1,63}(?<!-))*\.[A-...