Tuesday, February 6, 2018

MVC File Upload using Client side and Server Side

Purpose :-

We can achieve file upload either using any of the below options

  • File Upload using html input file field with Jquery Ajax to call server side web api
  • File Upload using MVC Razor Html.BeginForm to call asp.net mvc acction controller or web api
Below code base cover above implementation.


Key technical specifications :-


  1. Enctype =multipart/form-data
  2. List if using out of box asp.net
  3. Request.Form pass through Ajax Jquery or some sort of js library to read file in asp.net
  4. MultiPartFormBoundary 
  5. ContentType MediaTypeHeaderValue("application/octet-stream")

Implementation




Tuesday, January 23, 2018

Dynamically load partial view using mvc action controller


Introduction
As we all know we can have partial views to do many thing. Here is one of the scenario. Say we have different workflow based on operation selection. Either we have static partial views with full pledge workflow embedded in it or you load workflow as partial view dynamically.

 

With Dynamic Approach

  1. We can inject partial view at runtime
  2. The mark up will be lean and light as it will be on-demand load.


For proof of concept I choose existing Asp.net MVC razor from vs.

 

About.cshml

 

@model WebApplication3.Models.FullAndPartialViewModel
@{
    ViewBag.Title = 
"My Master Page";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
 
    $(document).ready(
function () {
        $(
"p").click(function () {
            
var categoryId = $("#ddlCategory").val();
           

            $(
"#dvCategoryResults").load("/home/InitiateWorkflow", { categoryId: categoryId });
        });
        $(
"#ddlCategory").change(function () {
            
var categoryId = $("#ddlCategory").val();
            $(
"#dvShowSubmissionCriteria").show();

            // $("#dvCategoryResults").load('@(Url.Action("GetCategoryProducts","Home",null, Request.Url.Scheme))?categoryId=' + categoryId);
           
//  $("#dvCategoryResults").load("/home/InitiateWorkflow", { categoryId: categoryId});
        });
    });
</script> 
<div id="container">
 
    
<label for="ddlCategory"><strong>Select a category</strong></label>
    
@Html.DropDownListFor(m => m.CategoryId,
        
new SelectList(Model.CategoryList, "CategoryId""CategoryName", Model.CategoryId), new { id = "ddlCategory", @class = "test" })
    
<br /><br />
    
<div id="dvShowSubmissionCriteria" hidden="hidden">
        
<p>This is submission criteria. Please accept before you proceed</p>
    
</div>
    
<p><strong>Click here to start Workflow -->.</strong></p>
    
<div id="dvCategoryResults">
 
    
</div>
</div>  

 

FullAndPartialViewModel.cs


using System.Collections.Generic;
 
namespace WebApplication3.Models
{
    
public class FullAndPartialViewModel
    {
        
public int CategoryId { getset; }
 
        
public List<CategoryListItem> CategoryList { getset; }
    }
    
public class  CategoryListItem
    {
        
public int CategoryId { getset; }
        
public string CategoryName { getset; }
 
    }
}


HomeController.cs

using System.Web.Mvc;
using WebApplication3.Models;
 
namespace WebApplication3.Controllers
{
    
public class HomeController : Controller
    {
        
public ActionResult Index()
        {
            
return View();
        }
 
        
public ActionResult About()
        {
            ViewBag.Message = 
"Your application description page.";
            
FullAndPartialViewModel viewModel = new FullAndPartialViewModel();
            viewModel.CategoryList = 
new System.Collections.Generic.List<CategoryListItem>();
            viewModel.CategoryList.Add(
new CategoryListItem() { CategoryId = 1, CategoryName = "Service Operation 1" });
            viewModel.CategoryList.Add(
new CategoryListItem() { CategoryId = 2, CategoryName = "Service Operation 2" });
            
return View(viewModel);
        }
       
        
public PartialViewResult InitiateWorkflow(int categoryId)
        {
            
//https://cmatskas.com/update-an-mvc-partial-view-with-ajax/
 
            
var service = new Service();
 
            
if (categoryId == 1)
            {
                service.Name = 
"Workflow 1";
                service.Value = 
"I got 5 Steps!";
                
return PartialView("~/Views/Home/_WF1.cshtml", service);
            }
            
else
            {
                service.Name = 
"Workflow 2";
                service.Value = 
"I got 100 Steps!";
                
return PartialView("~/Views/Home/_WF2.cshtml", service);
            }
        }
    }
}

 

Service.cs

namespace WebApplication3.Models
{
    
public class Service
    {
        
public string Name { getset; }
        
public string Value { getset; }
    }
}


Partial View Workflow 1: _WF1.cshtml

 

@using WebApplication3.Models
@model Service
<p>@Model.Name</p>
<p>@Model.Value</p>

 
Partial View Workflow 2: _WF2.cshtml

 

@using WebApplication3.Models
@model Service
<p>@Model.Name</p>
<p>@Model.Value</p>

 

Output
 

Machine generated alternative text:
Application name Home About 
Select a category Service Operation 2 
Contact 
This is submission criteria. Please accept before you proceed 
Click here to start Workflow 
Workflow 2 
I got 100 Steps! 
C 2018 MY ASRNET Application

Monday, December 18, 2017

Powershell Read XML config


Objective

  1. Read XML file
  2. Global variable





You can download code from gist.


Tuesday, December 12, 2017

Powershell Set IIS apppool custom identity credential


















import-module webadministration

function Set-WebsiteAppPoolCustomIdentity([String] $appPoolName, [String] $userId, [String] $pwd)

    Write-Host $appPoolName;
    $webAppPool = get-item IIS:\AppPools\$appPoolName     
    $webAppPool.processModel.userName = $userId;
    $webAppPool.processModel.password = $pwd;
    $webAppPool.processModel.identityType = 3;
    $webAppPool | Set-Item
    $webAppPool.Stop();
    $webAppPool.Start();
    Write-Host "IIS Recycled";
    $webAppPool = get-item iis:\apppools\$appPoolName;
    write-host "New Pool User: " $webAppPool.processModel.userName;
    write-host "New Pool PWd: " $webAppPool.processModel.password;
}

Set-WebsiteAppPoolCustomIdentity  -appPoolName "abc" -userId "hello" -pwd "world"



Wednesday, November 22, 2017

Microsoft Azure Discovery Day in Melbourne- Indeed a good show

I urge folks out there in Australia to leverage this wonderful opportunity to gather know and how about Azure. Too many things to catch up. Very fast pace of improvement, new services , new products Microsoft Azure is offering on the table so far in terms of PAAS, IAAS and SAAS. Having said that I attended Microsoft Azure Discovery Day in Melbourne on 21st Nov 2017. I liked the presentation , demo and short knowledge transitioned by presenter. I must say there is many thing happening in Azure space.


  • Continuous Delivery as part of App Service
  • Logic App
  • Azure Function
  • Web Apps
  • Automation Run book
  • Security Centre
  • Cloudyn for cost and estimation 
  • OMS
  • App Insight
  • Azure Template

  • [You can now mock api data without being backend ready. You can test api without using developer portal.
    Swagger definition available as part of code editor.]


References:




Thursday, November 9, 2017

Sitecore Dynamic Placeholder

I prefer

  • TDS
  • TDS with glass mapper template[Auto code generation for sitecore template model]
  • Fortis Dynamic Placeholder Nuget
  • Sitecore 8.2.1
  • MVC 5
  • .Net framework 4.5.2

The difficult part of sitecore is setup stage to start your development.

Install MVC Sitecore 8.2.1
Sitecore solution base nuget package can be found in above documentation. Follow step by step instruction.

Install Glass mapper

Install Dynamic Placeholder Nuget




Install TDS

Use wizard to create master content tree sync solution

Sitecore .Net Solution

 

Glass mapper TDS - Template










































Sitecore Rocks

Dynamic Placeholder

Glass Mapper @editable View Rendering


To create this sample. I have created two component in MVC and then corresponding renderings in sitecore.

View  as Component
Placeholder
Sitecore Renderings
1
MainLayoutPage.cshtml

<html>
<body>
      
<div class="container">
        
@Html.Sitecore().DynamicPlaceholder("dynamic-row")
    
</div>
</body>
</html>

dynamic-row

2
OneRowThreeColumn.cshtml

<div class="row">
    
<div class="col-sm-4">
        
<div class="tile--bordered">
            
@Html.Sitecore().DynamicPlaceholder("dynamic-content")
        
</div>
    
</div>
    
<div class="col-sm-4">
        
<div class="tile--bordered">
            
@Html.Sitecore().DynamicPlaceholder("dynamic-content")
        
</div>
    
</div>
    
<div class="col-sm-4">
        
<div class="tile--bordered">
            
@Html.Sitecore().DynamicPlaceholder("dynamic-content")
        
</div>
    
</div>
</div>

dynamic-row
One Row Three Column

3
RichText.cshtml

@inherits Glass.Mapper.Sc.Web.Mvc.GlassViewISingleRichText>

<p class="tile__title">@Editable(Model, y=>y.Title)</p>
<p class="tile__sub-title">
    
@if (Model != null)
    {
        
@Editable(Model, y => y.Detail)
    }
</p>

 

dynamic-content
***Rich Text with Model(Template- Data Template SingleRichText)

Create following items in sitecore:

  1. Layout
  2. Data Template
  3. Renderings
  4. Placeholder setting
  5. Page item in content tree

 

  1. Layout: MainLayout



2.0 Placeholder Settings: dynamic-row



2.1 Placeholder Settings: dynamic-content


 

3.0 Renderings: One Row Three Column



 

3.1 Renderings: Rich Text



 

 

3.2 Data Template





  1. Content Tree


  1.  Presentation: Shared Layout


 

  1.  Experience Editor







 

  1. Presentation: Post Publish- Final Rendering