<#@ template language="C#" HostSpecific="True" #>
<#@ output extension="vb" #>
<#@ include file="..\..\ControllerHead.vb.include.t4" #>
Imports System.Web
Imports System.Data
Imports System.Web.Mvc
Imports System.Data.Entity

' This code was generated by C1 Scaffolder.


Public class <#= ControllerName #>
    Inherits Controller

    Private db As <#= DbContextTypeName #> = New <#= DbContextTypeName #>()
<# 
var selectedModelProperties = Options["SelectedModelProperties"];
var paramWithType = "";
var param = "";
var paramName = "key";
var bindParam = "";
var viewDataInstanceName = ViewDataTypeName.Substring(ViewDataTypeName.LastIndexOf('.') + 1).ToLower();
int i = 1;
var fields = Options["Fields"];
var columns = fields;
foreach (var primaryKey in PrimaryKeys)
{
    paramWithType = paramWithType + paramName + i + " As " + primaryKey.Value + ",";
    param = param + paramName + i + ",";
    i++;
}
foreach(var property in selectedModelProperties)
{
    bindParam += property["Name"] + ",";
}
param = param.Substring(0, param.Length - 1);
paramWithType = paramWithType.Substring(0, paramWithType.Length - 1);
bindParam = bindParam.Substring(0, bindParam.Length - 1);
#>
    Public Function Index() As ActionResult
        Dim model = db.<#= EntitySetName #>
        Return View(model)
    End Function

<#@ include file="..\..\DataMapAction.vb.include.t4" #>

    Public Function Details(<#= paramWithType #>) As ActionResult
        Dim model = db.<#= EntitySetName #>.Find(<#= param#>)
        If model Is Nothing Then
            Return HttpNotFound()
        End If
        Return View(model)
    End Function

    Public Function Create() As ActionResult
<#@ include file="..\..\DataMapViewBag.vb.include.t4" #>

        Return View()
    End Function

    <HttpPost> _
    <ValidateAntiForgeryToken> _
    Public Function Create(<Bind(Include := "<#=bindParam#>")> <#=viewDataInstanceName#> As <#= ViewDataTypeName #>) As ActionResult
        If ModelState.IsValid Then
            db.<#= EntitySetName #>.Add(<#=viewDataInstanceName#>)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(<#=viewDataInstanceName#>)
    End Function

    Public Function Edit(<#= paramWithType #>) As ActionResult
        Dim model = db.<#= EntitySetName #>.Find(<#= param#>)
        If model Is Nothing Then
            Return HttpNotFound()
        End If

<#@ include file="..\..\DataMapViewBag.vb.include.t4" #>

        Return View(model)
    End Function

    <HttpPost> _
    <ValidateAntiForgeryToken> _
    Public Function Edit(<Bind(Include := "<#=bindParam#>")> <#=viewDataInstanceName#> As <#= ViewDataTypeName #>) As ActionResult
        If ModelState.IsValid Then
            db.Entry(<#=viewDataInstanceName#>).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If
        Return View(<#=viewDataInstanceName#>)
    End Function

    Public Function Delete(<#= paramWithType #>) As ActionResult
        Dim model = db.<#= EntitySetName #>.Find(<#= param#>)
        If model Is Nothing Then
            Return HttpNotFound()
        End If

        Return View(model)
    End Function

    <HttpPost, ActionName("Delete")> _
    <ValidateAntiForgeryToken> _
    Public Function DeleteConfirmed(<#= paramWithType #>) As ActionResult
        Dim model = db.<#= EntitySetName #>.Find(<#= param#>)
        db.<#= EntitySetName #>.Remove(model)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

    Protected Overrides Sub Dispose(disposing As Boolean)
        If disposing Then
            db.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

End Class