Nested Queries in Linq - linq

I have my table structure like this.
ContinentTable => CountryTable => UserTable
User has CountryId. Country has ContinentId.
I want to query all the continents with the number of users in each.
I achieved this via SQL. But I want to use linq extension method since Im working with EF Core.
Here is my sql query
SELECT Continent.Id, Continent.Name, COUNT(Usr.UserName) AS 'User Count'
FROM Continents Continent, Countries Country, Users Usr
WHERE Continent.Id = Country.ContinentId
AND Country.Id = Usr.CountryId
GROUP BY Continent.Id, Continent.Name

If this is your object model:
Dim continents As Dictionary(Of String, Dictionary(Of String, List(Of String)))
continents = New Dictionary(Of String, Dictionary(Of String, List(Of String))) From
{
{"Europe", New Dictionary(Of String, List(Of String)) From
{
{"France", New List(Of String) From {"Antoine", "Serge"}},
{"Germany", New List(Of String) From {"Hans", "Wolfgang"}},
{"Spain", New List(Of String) From {"Pablo", "Pedro"}}
}
},
{"America", New Dictionary(Of String, List(Of String)) From
{
{"USA", New List(Of String) From {"Kevin", "Caleb"}},
{"Mexico", New List(Of String) From {"Gustavo", "Oscar"}},
{"Brazil", New List(Of String) From {"Alexandro"}}
}
}
}
Then this would be your LINQ query:
Dim result As IEnumerable(Of KeyValuePair(Of String, Integer)) = continents.Select(Of KeyValuePair(Of String, Integer))(Function(x As KeyValuePair(Of String, Dictionary(Of String, List(Of String)))) New KeyValuePair(Of String, Integer)(x.Key, x.Value.Select(Of Integer)(Function(y As KeyValuePair(Of String, List(Of String))) y.Value.Count).Sum))

Related

ORA-01460: unimplemented or unreasonable conversion (after conversion of a docx to pdf and uploading to db) VisualBasic (VB)

i'm a junior developer and i started working recently.
They put me on this LEGACY program and asked me to try and resolve this problem.
They strictly forbid me of using CLOB.
Dim fs As Stream = PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
pdfComunication = pdfConvert(bytes)
pdfComunication = bytes
idOperator = ViewState("idOperator")
'
Private Function pdfConvert(ByVal document As Byte()) As Byte()
Dim doc As Byte()
Using msRtf As MemoryStream = New MemoryStream(document)
Dim dc As softDocument.DocumentCore = softDocument.DocumentCore.Load(msRtf, New softDocument.DocxLoadOptions())
Using msDocx As MemoryStream = New MemoryStream()
dc.Save(msDocx, New softDocument.PdfSaveOptions())
doc = msDocx.ToArray()
End Using
End Using
Return doc
End Function
'
<WebMethod()>
Public Function InsertComunications(ByVal pIdComunicationType As Integer, ByVal pAwaitingStatus As Integer, ByVal pRequestingUser As String, ByVal pIdSr As String,
ByVal pItem As String, ByVal pText As String, ByVal pCommonSender As String, pPecSender As String, ByVal pDestination As String,
ByVal pPdfComunication As Byte()) As String
Dim dbConn As New OracleConnection
Dim cmd As New OracleCommand
Dim errMsg As String = String.Empty
dbConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("DBConn").ToString
Try
dbConn.Open()
cmd.Parameters.Clear()
cmd.CommandText = ""
cmd.CommandType = System.Data.CommandType.StoredProcedure
cmd.Connection = dbConn
Dim p_IdComunicationType As New System.Data.OracleClient.OracleParameter("p_IdComunicationType ", System.Data.OracleClient.OracleType.Number)
p_IdComunicationType.Direction = ParameterDirection.Input
p_IdComunicationType.Value = pIdComunicationType
cmd.Parameters.Add(p_IdComunicationType)
Dim p_AwaitingStatus As New System.Data.OracleClient.OracleParameter("p_AwaitingStatus", System.Data.OracleClient.OracleType.Number)
p_AwaitingStatus.Direction = ParameterDirection.Input
p_AwaitingStatus.Value = pAwaitingStatus
cmd.Parameters.Add(p_AwaitingStatus)
Dim p_RequestingUser As New System.Data.OracleClient.OracleParameter("p_RequestingUser ", System.Data.OracleClient.OracleType.NVarChar)
p_RequestingUser.Direction = ParameterDirection.Input
p_RequestingUser.Value = pRequestingUser
cmd.Parameters.Add(p_RequestingUser)
Dim p_IdSr As New System.Data.OracleClient.OracleParameter("p_IdSr", System.Data.OracleClient.OracleType.NVarChar)
p_IdSr.Direction = ParameterDirection.Input
p_IdSr.Value = pIdSr
cmd.Parameters.Add(p_IdSr)
Dim p_Item As New System.Data.OracleClient.OracleParameter("p_Item", System.Data.OracleClient.OracleType.NVarChar)
p_Item.Direction = ParameterDirection.Input
p_Item.Value = pItem
cmd.Parameters.Add(p_Item)
Dim p_Text As New System.Data.OracleClient.OracleParameter("p_Text", System.Data.OracleClient.OracleType.NVarChar)
p_Text.Direction = ParameterDirection.Input
p_Text.Value = pText
cmd.Parameters.Add(p_Text)
Dim p_CommonSender As New System.Data.OracleClient.OracleParameter("p_CommonSender", System.Data.OracleClient.OracleType.NVarChar)
p_CommonSender.Direction = ParameterDirection.Input
p_CommonSender.Value = pCommonSender
cmd.Parameters.Add(p_CommonSender)
Dim p_PecSender As New System.Data.OracleClient.OracleParameter("p_PecSender", System.Data.OracleClient.OracleType.NVarChar)
p_PecSender.Direction = ParameterDirection.Input
p_PecSender.Value = pPecSender
cmd.Parameters.Add(p_PecSender)
Dim p_Destination As New System.Data.OracleClient.OracleParameter("p_Destination", System.Data.OracleClient.OracleType.NVarChar)
p_Destination.Direction = ParameterDirection.Input
p_Destination.Value = pDestination
cmd.Parameters.Add(p_Destination)
Dim p_PdfComunication As New System.Data.OracleClient.OracleParameter("p_PdfComunication", System.Data.OracleClient.OracleType.Blob)
p_PdfComunication.Direction = ParameterDirection.InputOutput
p_PdfComunication.Value = pPdfComunication
cmd.Parameters.Add(p_PdfComunication)
cmd.ExecuteNonQuery()
Finally
dbConn.Dispose()
cmd.Dispose()
End Try
'
The problem is that the file size is too big for the blob in the db, which is strange since in other parts of the program that don't do the conversion it takes files way bigger than the ones i'm trying right now...
Any suggestions?
'EDIT
TYPE t_Cursor IS REF CURSOR;
PROCEDURE GetMailTemplate(Cur_Out OUT t_Cursor);
PROCEDURE 1(p_IdComunicationType IN NUMBER,
p_AwaitingStatus IN NUMBER,
p_RequestingUser IN VARCHAR2,
p_IdSr IN VARCHAR2,
p_Item IN VARCHAR2,
p_Testo IN VARCHAR2,
p_CommonSender IN VARCHAR2,
p_PecSender IN VARCHAR2,
p_Destination IN VARCHAR2,
p_PdfComunication xx.PDF_COMUNICATION%Type
);
PROCEDURE 2( p_AwaytingStatus NUMBER, Cur_Out OUT t_Cursor);
PROCEDURE 3(p_IdComunicationType VARCHAR2,
p_AwaitingStatus NUMBER);
We finally found an answer, we had to put everything inside a TRANSACTION:
Public Function InsertComunications(ByVal pIdComunicationType As Integer, ByVal pAwaitingStatus As Integer, ByVal pRequestingUser As String, ByVal pIdSr As String,
ByVal pItem As String, ByVal pText As String, ByVal pCommonSender As String, pPecSender As String, ByVal pDestination As String,
ByVal pPdfComunication As Byte()) As String
Dim dbConn As New OracleConnection
**Dim trns as System.Data.OracleClient.OracleTransaction = Nothing**
Dim cmd As New OracleCommand
Dim errMsg As String = String.Empty
dbConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("DBConn").ToString
Try
dbConn.Open()
**trns = dbConn.BeginTransaction**
**cmd.Transaction = trns**
cmd.Parameters.Clear()
cmd.CommandText = ""
cmd.CommandType = System.Data.CommandType.StoredProcedure
cmd.Connection = dbConn
Dim p_IdComunicationType As New System.Data.OracleClient.OracleParameter("p_IdComunicationType ", System.Data.OracleClient.OracleType.Number)
p_IdComunicationType.Direction = ParameterDirection.Input
p_IdComunicationType.Value = pIdComunicationType
cmd.Parameters.Add(p_IdComunicationType)
Dim p_AwaitingStatus As New System.Data.OracleClient.OracleParameter("p_AwaitingStatus", System.Data.OracleClient.OracleType.Number)
p_AwaitingStatus.Direction = ParameterDirection.Input
p_AwaitingStatus.Value = pAwaitingStatus
cmd.Parameters.Add(p_AwaitingStatus)
Dim p_RequestingUser As New System.Data.OracleClient.OracleParameter("p_RequestingUser ", System.Data.OracleClient.OracleType.NVarChar)
p_RequestingUser.Direction = ParameterDirection.Input
p_RequestingUser.Value = pRequestingUser
cmd.Parameters.Add(p_RequestingUser)
Dim p_IdSr As New System.Data.OracleClient.OracleParameter("p_IdSr", System.Data.OracleClient.OracleType.NVarChar)
p_IdSr.Direction = ParameterDirection.Input
p_IdSr.Value = pIdSr
cmd.Parameters.Add(p_IdSr)
Dim p_Item As New System.Data.OracleClient.OracleParameter("p_Item", System.Data.OracleClient.OracleType.NVarChar)
p_Item.Direction = ParameterDirection.Input
p_Item.Value = pItem
cmd.Parameters.Add(p_Item)
Dim p_Text As New System.Data.OracleClient.OracleParameter("p_Text", System.Data.OracleClient.OracleType.NVarChar)
p_Text.Direction = ParameterDirection.Input
p_Text.Value = pText
cmd.Parameters.Add(p_Text)
Dim p_CommonSender As New System.Data.OracleClient.OracleParameter("p_CommonSender", System.Data.OracleClient.OracleType.NVarChar)
p_CommonSender.Direction = ParameterDirection.Input
p_CommonSender.Value = pCommonSender
cmd.Parameters.Add(p_CommonSender)
Dim p_PecSender As New System.Data.OracleClient.OracleParameter("p_PecSender", System.Data.OracleClient.OracleType.NVarChar)
p_PecSender.Direction = ParameterDirection.Input
p_PecSender.Value = pPecSender
cmd.Parameters.Add(p_PecSender)
Dim p_Destination As New System.Data.OracleClient.OracleParameter("p_Destination", System.Data.OracleClient.OracleType.NVarChar)
p_Destination.Direction = ParameterDirection.Input
p_Destination.Value = pDestination
cmd.Parameters.Add(p_Destination)
Dim p_PdfComunication As New System.Data.OracleClient.OracleParameter("p_PdfComunication", System.Data.OracleClient.OracleType.Blob)
p_PdfComunication.Direction = ParameterDirection.InputOutput
p_PdfComunication.Value = pPdfComunication
cmd.Parameters.Add(p_PdfComunication)
cmd.ExecuteNonQuery()
**trns.Commit()**
Finally
dbConn.Dispose()
cmd.Dispose()
End Try

in and out oracle parameters in vb.net result in null errors

so this code works with just the out parameter but then when you add in parameters weird nulls start showing up and also the straight in parameters work too so it is just when there are both in and out parameters.
the part with problems is the in and out parameters do not want to coexist
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
For Each kvp As KeyValuePair(Of String, String) In sqlParams
Dim newParam As New OracleParameter(kvp.Key, OracleDbType.Varchar2, kvp.Value, Data.ParameterDirection.Output)
newParam.Size = 400
cmd.Parameters.Add(newParam)
Next
This works
Public Shared Sub Run_Oracle_Query(ByVal queryToRun As String, ByVal sqlParams As Dictionary(Of String, String))
Dim dbConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ITSS").ConnectionString
Dim con As OracleConnection = New OracleConnection(dbConnString)
Dim cmd = con.CreateCommand()
Try
con.Open()
cmd.CommandText = queryToRun
cmd.CommandType = CommandType.Text
For Each kvp As KeyValuePair(Of String, String) In sqlParams
cmd.Parameters.Add(kvp.Key, kvp.Value)
Next
cmd.ExecuteNonQuery()
'log all sql queryies very expensive operation
LogThisString("Log All queries: " & queryToRun, "Always")
Catch ex As OracleException ' catches only Oracle errors
OracleExceptionLogging(ex.Number, queryToRun, ex)
Catch ex As Exception
LogThisString("General Error SQL: " + ex.Message.ToString(), "Always")
' MsgBox("Could Not Perform This Database Operation")
Finally
con.Close()
cmd.Dispose()
con.Dispose()
End Try
End Sub
This works
Public Shared Function Run_Insert_Oracle_Query_Return_ID(ByVal queryToRun As String, ByVal IDcolumnName As String) As String
Dim dbConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ITSS").ConnectionString
Dim con As OracleConnection = New OracleConnection(dbConnString)
Dim cmd = con.CreateCommand()
Dim strUkey As String = ""
Try
con.Open()
cmd.CommandText = queryToRun & " RETURNING " & IDcolumnName & " INTO :UKEY"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
cmd.ExecuteNonQuery()
strUkey = cmd.Parameters("PKEY").Value.ToString
Return strUkey
Catch ex As OracleException ' catches only Oracle errors
OracleExceptionLogging(ex.Number, queryToRun, ex)
Catch ex As Exception
LogThisString("General Error SQL: " + ex.Message.ToString(), "Always")
PopupMsgBox("Database Error", "Database Integrity Constants Violated SQL Operation Will Not Be Executed")
Finally
con.Close()
cmd.Dispose()
con.Dispose()
End Try
Return strUkey
End Function
This does not work
Public Shared Function Run_Insert_Oracle_Query_Return_ID(ByVal queryToRun As String, ByVal IDcolumnName As String, ByVal sqlParams As Dictionary(Of String, String)) As String
Dim dbConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ITSS").ConnectionString
Dim con As OracleConnection = New OracleConnection(dbConnString)
Dim cmd = con.CreateCommand()
Dim strUkey As String = ""
Try
con.Open()
cmd.CommandText = queryToRun & " RETURNING " & IDcolumnName & " INTO :UKEY"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)
For Each kvp As KeyValuePair(Of String, String) In sqlParams
Dim newParam As New OracleParameter(kvp.Key, OracleDbType.Varchar2, kvp.Value, Data.ParameterDirection.Output)
newParam.Size = 400
cmd.Parameters.Add(newParam)
Next
cmd.ExecuteNonQuery()
strUkey = cmd.Parameters("PKEY").Value.ToString
Return strUkey
Catch ex As OracleException ' catches only Oracle errors
OracleExceptionLogging(ex.Number, queryToRun, ex)
Catch ex As Exception
LogThisString("General Error SQL: " + ex.Message.ToString(), "Always")
PopupMsgBox("Database Error", "Database Integrity Constants Violated SQL Operation Will Not Be Executed")
Finally
con.Close()
cmd.Dispose()
con.Dispose()
End Try
Return strUkey
End Function
crap so i guess it does look for order asnd the output does need to be at end like this.....this works solved my own problem
For Each kvp As KeyValuePair(Of String, String) In sqlParams
Dim newParam As New OracleParameter(kvp.Key, OracleDbType.Varchar2, kvp.Value, Data.ParameterDirection.Input)
newParam.Size = 400
cmd.Parameters.Add(newParam)
Next
cmd.Parameters.Add("PKEY", OracleDbType.Int64, ParameterDirection.Output)

MVC3 VB Razor not recognizing converted C# HTML extension method code

I've been looking through Google search results for ways to implement some sort of RadioButton list (VB, MVC3, Razor). Unfortunately, most of the results I find are in C#; this is the most useful reference I can find: Jon Lanceley's MVC3 RadioButton Helper (C#, by the way), so I needed to translate the code into VB (thanks, developerFusion!).
I thought it would be fairly straighforward, but I discovered that the compiler doesn't recognize the function as part of the module namespace; some attempts at working around the problem ensued, but failed.
Here's my code (you can see all the attempts I've tried via comments, signed with Andrew.San if you Ctrl + F it):
Helper:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Mvc.Html
Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices
Imports System.Text
Namespace RadioButtonListForHelperTest
Public Module HtmlExtensions
Sub New()
End Sub
<Extension()> _
Public Function RadioButtonForSelectList(Of TModel, TProperty)( _
htmlHelper As HtmlHelper(Of TModel), _
expression As Expression(Of Func(Of TModel, TProperty)), _
listOfValues As IEnumerable(Of SelectListItem)) As MvcHtmlString
Dim metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData)
Dim stringBuilder = New StringBuilder()
If listOfValues IsNot Nothing Then
' Create a radio button for each item in the list
For Each item As SelectListItem In listOfValues
' Generate an id to be given to the radio button field
Dim id = String.Format("{0}_{1}", metaData.PropertyName, item.Value)
' Create and populate a radio button using the existing html helpers
Dim label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text))
Dim radio = htmlHelper.RadioButtonFor(expression, item.Value, New With { _
Key .id = id _
}).ToHtmlString()
' Create the html string that will be returned to the client
' e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label>
stringBuilder.AppendFormat("<div class="RadioButton">{0}{1}</div>", radio, label)
Next
End If
Return MvcHtmlString.Create(stringBuilder.ToString())
End Function
End Module
End Namespace
Model:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.ComponentModel.DataAnnotations
Imports System.Web.Mvc
Namespace RadioButtonListForHelperTest.Models
Public Class TestModel
Public Property TestRadioList() As IEnumerable(Of SelectListItem)
Get
Return m_TestRadioList
End Get
Set(value As IEnumerable(Of SelectListItem))
m_TestRadioList = value
End Set
End Property
Private m_TestRadioList As IEnumerable(Of SelectListItem)
Public Property TestRadioList2() As IEnumerable(Of SelectListItem)
Get
Return m_TestRadioList2
End Get
Set(value As IEnumerable(Of SelectListItem))
m_TestRadioList2 = value
End Set
End Property
Private m_TestRadioList2 As IEnumerable(Of SelectListItem)
<Display( _
Name:="Test Radio 1", _
Description:="This is the first list.", _
Prompt:="Some prompt message.", _
Order:=2)> _
<Required(ErrorMessage:="You must select an option for TestRadio")> _
Public Property TestRadio() As String
Get
Return m_TestRadio
End Get
Set(value As String)
m_TestRadio = value
End Set
End Property
Private m_TestRadio As String
<Display( _
Name:="Test Radio 2", _
Description:="This is the second list.", _
Prompt:="Some prompt message.", _
Order:=1)> _
<Required(ErrorMessage:="You must select an option for TestRadio2")> _
Public Property TestRadio2() As String
Get
Return m_TestRadio2
End Get
Set(value As String)
m_TestRadio2 = value
End Set
End Property
Private m_TestRadio2 As String
' I tried it out, just in case
' (see Index; #Html.RadioButtonForSelectList).
' It didn't help, really.
' - Andrew.San
Public Function RadioButtonForSelectList(Of TModel, TProperty)( _
htmlHelper As HtmlHelper(Of TModel), _
expression As Expressions.Expression(Of Func(Of TModel, TProperty)), _
listOfValues As IEnumerable(Of SelectListItem)) As MvcHtmlString
Return RadioButtonListForHelperTest.RadioButtonForSelectList(htmlHelper, expression, listOfValues)
End Function
End Class
Public Class aTest
Public Property ID() As Integer
Get
Return m_ID
End Get
Set(value As Integer)
m_ID = value
End Set
End Property
Private m_ID As Integer
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = value
End Set
End Property
Private m_Name As String
End Class
End Namespace
Controller:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models
Namespace Controllers
Public Class TestController
Inherits System.Web.Mvc.Controller
'
' GET: /Test
Function Index() As ActionResult
Dim list As New List(Of aTest)()
list.Add(New aTest() With { _
.ID = 1, _
.Name = "Line1" _
})
list.Add(New aTest() With { _
.ID = 2, _
.Name = "Line2" _
})
list.Add(New aTest() With { _
.ID = 3, _
.Name = "Line3" _
})
Dim sl As New SelectList(list, "ID", "Name")
Dim list2 As New List(Of aTest)()
list2.Add(New aTest() With { _
.ID = 1, _
.Name = "test1" _
})
list2.Add(New aTest() With { _
.ID = 2, _
.Name = "test2" _
})
Dim sl2 As New SelectList(list2, "ID", "Name")
Dim model = New TestModel()
model.TestRadioList = sl
model.TestRadioList2 = sl2
model.TestRadio = "2"
' Set a default value for the first radio button helper
Return View(model)
End Function
'
' POST: /Test
<HttpPost()> _
Public Function Index(model As TestModel, returnUrl As String) As ActionResult
If ModelState.IsValid Then
ModelState.AddModelError("", "Always force an error to be raised so we can test the postback sets the radio buttons to their last values.")
End If
' If we got this far, something failed, redisplay form
Dim list As New List(Of aTest)()
list.Add(New aTest() With { _
.ID = 1, _
.Name = "Line1" _
})
list.Add(New aTest() With { _
.ID = 2, _
.Name = "Line2" _
})
list.Add(New aTest() With { _
.ID = 3, _
.Name = "Line3" _
})
Dim sl As New SelectList(list, "ID", "Name")
Dim list2 As New List(Of aTest)()
list2.Add(New aTest() With { _
.ID = 1, _
.Name = "test1" _
})
list2.Add(New aTest() With { _
.ID = 2, _
.Name = "test2" _
})
Dim sl2 As New SelectList(list2, "ID", "Name")
model.TestRadioList = sl
model.TestRadioList2 = sl2
Return View(model)
End Function
End Class
End Namespace
View:
#Imports RadioButtonListForHelperTest <!-- Andrew.San - Looked up Google on how to import a namespace in VB -->
#ModelType RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel
#Code
Dim obj1 As System.Web.Mvc.HtmlHelper(Of RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel)
Dim obj2 As System.Web.Mvc.HtmlHelper(Of RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel)
ViewData("Title") = "Index"
End Code
<h2>Index</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#Using Html.BeginForm()
#Html.ValidationSummary(True)
#<fieldset>
<legend>TestModel</legend>
<div class="editor-label">
#Html.LabelFor(Function(model) model.TestRadio)
</div>
<div class="editor-field">
#Html.RadioButtonForSelectList(Function(model) model.TestRadio, Model.TestRadioList)
<!-- Andrew.San
Apparently, the method isn't a member of
System.Web.Mvc.HtmlHelper(Of RadioButtonListForHelperTest.RadioButtonListForHelperTest.Models.TestModel),
which is odd; I don't see the need for a Model to have this method.
-->
#RadioButtonListForHelperTest.RadioButtonListForHelperTest.RadioButtonForSelectList(obj1, Function(model) model.TestRadio, Model.TestRadioList)
<!-- Andrew.San
The compiler needs me to send an htmlHelper argument to the
HtmlExtension RadioButtonForSelectList method,
which Jon Lanceley didn't need to do.
#Html.RadioButtonForSelectList(m => m.TestRadio, Model.TestRadioList)
-->
#Html.ValidationMessageFor(Function(model) model.TestRadio)
</div>
<div class="editor-label">
#Html.LabelFor(Function(model) model.TestRadio2)
</div>
<div class="editor-field">
<!-- Andrew.San
As per the first radio button list.
-->
#Html.RadioButtonForSelectList(Function(model) model.TestRadio2, Model.TestRadioList2)
#RadioButtonListForHelperTest.RadioButtonListForHelperTest.RadioButtonForSelectList(obj2, Function(model) model.TestRadio2, Model.TestRadioList2)
#Html.ValidationMessageFor(Function(model) model.TestRadio2)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Right. That's all I've got.
The problem is that your extension method is in the RadioButtonListForHelperTest namespace, but in your view you attempt to access it view the System.Web.Mvc.Html namespace. #Html is in System.Web.Mvc.
To access your extension method in the same namespace you need to do the following.
Change your current namespace to System.Web.Mvc.Html. Then create a static class named whatever you like, MyExtensions and then include you extension method in this class. It must be static.

custom htmlhelper with validation support

I'm rockin my own custom HtmlHelper to enable AutoComplete support on a Select List.
This is working alright, except I need to be able to support the DataAnnotations in my ViewModel.
Here's my current (working) HtmlHelper (sans validation)
<Extension()>
Public Function AutoCompleteDropDownList(ByVal helper As HtmlHelper, name As String, autoCompleteSelectListItem As List(Of AutoCompleteSelectListItem), htmlAttributes As Object) As MvcHtmlString
Dim selectBuilder As New TagBuilder("select")
selectBuilder.MergeAttribute("name", name)
selectBuilder.MergeAttributes(New RouteValueDictionary(htmlAttributes))
selectBuilder.MergeAttribute("autocorrect", "off")
selectBuilder.MergeAttribute("autocomplete", "off")
Dim selectListBuilder As New TagBuilder("option")
selectListBuilder.MergeAttribute("value", "")
selectListBuilder.MergeAttribute("selected", "selected")
Dim innerHtmlBuilder As New StringBuilder
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
For Each item In autoCompleteSelectListItem
selectListBuilder = New TagBuilder("option")
selectListBuilder.MergeAttribute("value", item.Value)
selectListBuilder.MergeAttribute("data-alternative-spellings", item.AlternativeSpellings)
selectListBuilder.MergeAttribute("data-relevancy-booster", item.RelevancyBooster)
selectListBuilder.InnerHtml = item.Label
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
Next
selectBuilder.InnerHtml = innerHtmlBuilder.ToString()
Return MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal))
End Function
How would one rewrite this to support validation? Something like AutoCompleteDropDownListFor()
PS: a C# solution is perfectly acceptable, the project I'm working on is in VB, but I don't mind translating.
PS: I'm flipping through the source code on http://aspnet.codeplex.com and I can't find any reference to DropDownListFor
You will have to download the source code and you will find the implementation of the DropDownListFor helper inside mvc3-rtm-sources.zip\mvc3-rtm-sources\mvc3\src\SystemWebMvc\Mvc\Html\SelectExtensions.cs. To enable client side validation you have to emit HTML5 data-* attributes on the drop down. This is done towards the end of the SelectInternal method by calling htmlHelper.GetUnobtrusiveValidationAttributes method.
<Extension()>
Public Function AutoCompleteDropDownList(ByVal helper As HtmlHelper, name As String, autoCompleteSelectListItem As List(Of AutoCompleteSelectListItem), htmlAttributes As Object) As MvcHtmlString
Dim selectBuilder As New TagBuilder("select")
selectBuilder.MergeAttribute("name", name)
selectBuilder.MergeAttributes(New RouteValueDictionary(htmlAttributes))
selectBuilder.MergeAttribute("autocorrect", "off")
selectBuilder.MergeAttribute("autocomplete", "off")
Dim selectListBuilder As New TagBuilder("option")
selectListBuilder.MergeAttribute("value", "")
selectListBuilder.MergeAttribute("selected", "selected")
Dim innerHtmlBuilder As New StringBuilder
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
For Each item In autoCompleteSelectListItem
selectListBuilder = New TagBuilder("option")
selectListBuilder.MergeAttribute("value", item.Value)
selectListBuilder.MergeAttribute("data-alternative-spellings", item.AlternativeSpellings)
selectListBuilder.MergeAttribute("data-relevancy-booster", item.RelevancyBooster)
selectListBuilder.InnerHtml = item.Label
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
Next
selectBuilder.InnerHtml = innerHtmlBuilder.ToString()
selectBuilder.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(name))
Return MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal))
End Function
UPDATE:
As requested in the comments section, here's how a strongly typed version of the helper would look like:
<Extension()>
Public Function AutoCompleteDropDownListFor(Of TModel, TProperty)(helper As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TProperty)), autoCompleteSelectListItem As List(Of AutoCompleteSelectListItem), htmlAttributes As Object) As IHtmlString
Dim name = ExpressionHelper.GetExpressionText(expression)
Dim fullHtmlFieldName As String = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name)
Dim selectBuilder = New TagBuilder("select")
selectBuilder.MergeAttribute("name", fullHtmlFieldName)
selectBuilder.MergeAttributes(New RouteValueDictionary(htmlAttributes))
selectBuilder.MergeAttribute("autocorrect", "off")
selectBuilder.MergeAttribute("autocomplete", "off")
Dim selectListBuilder = New TagBuilder("option")
selectListBuilder.MergeAttribute("value", "")
selectListBuilder.MergeAttribute("selected", "selected")
Dim innerHtmlBuilder = New StringBuilder()
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
For Each item In autoCompleteSelectListItem
selectListBuilder = New TagBuilder("option")
selectListBuilder.MergeAttribute("value", item.Value)
selectListBuilder.MergeAttribute("data-alternative-spellings", item.AlternativeSpellings)
selectListBuilder.MergeAttribute("data-relevancy-booster", item.RelevancyBooster)
selectListBuilder.InnerHtml = item.Label
innerHtmlBuilder.Append(selectListBuilder.ToString(TagRenderMode.Normal))
Next
selectBuilder.InnerHtml = innerHtmlBuilder.ToString()
selectBuilder.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(name))
Return MvcHtmlString.Create(selectBuilder.ToString(TagRenderMode.Normal))
End Function

htmlhelper inside webgrid

I'm using MVC3 and I need a HTMLHelper with image, so I found a nice code, the problem is that the image is not shown instead of I see html code ex:
<a href='whatever'><img src="images/printer.png" /></a>
Maybe this is because the helper is inside a webgrid helper, I really don't know, any suggestion?
Helper
<Extension()> _
Public Function ActionLinkWithImage(ByVal helper As HtmlHelper, ByVal actionName As String, ByVal controllerName As String, ByVal imageUrl As String, ByVal alternateText As String, ByVal routeValues As Object, ByVal linkHtmlAttributes As Object, ByVal imageHtmlAttributes As Object) As String
Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext)
Dim url = urlHelper.Action(actionName, controllerName, routeValues)
' Create link
Dim linkTagBuilder = New TagBuilder("a")
linkTagBuilder.MergeAttribute("href", url)
linkTagBuilder.MergeAttributes(New RouteValueDictionary(linkHtmlAttributes))
' Create image
Dim imageTagBuilder = New TagBuilder("img")
imageTagBuilder.MergeAttribute("src", urlHelper.Content(imageUrl))
imageTagBuilder.MergeAttribute("alt", urlHelper.Encode(alternateText))
imageTagBuilder.MergeAttributes(New RouteValueDictionary(imageHtmlAttributes))
' Add image to link
linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing)
Return linkTagBuilder.ToString()
End Function
WebGrid load
#<div id="divGrid">
#grid.GetHtml(
tableStyle:="webgrid",
headerStyle:="webgrid-header",
footerStyle:="webgrid-footer",
alternatingRowStyle:="webgrid-alternating-row",
selectedRowStyle:="webgrid-selected-row",
rowStyle:="webgrid-row-style",
mode:=WebGridPagerModes.All,
firstText:="<< Inicio",
previousText:="< Anterior",
nextText:="Siguiente >",
lastText:="Fin >>",
columns:=grid.Columns(
grid.Column(format:=Function(Model) Html.ActionLink(" ", "Details", "CertificadoVehiculo", New With {.area = "Certificados", .parIDCertificado = Model.ID_CERTIFICADO, .parIDPoliza = Model.ID_POLIZA}, New With {.class = "imgConsultar", .title = "Consultar"}), style:="webgrid-width-images"),
grid.Column(format:=Function(Model) Html.ActionLink(" ", "Edit", "CertificadoVehiculo", New With {.area = "Certificados", .parIDCertificado = Model.ID_CERTIFICADO, .parIDPoliza = Model.ID_POLIZA}, New With {.class = "imgEditar", .title = "Modificar"}), style:="webgrid-width-images"),
grid.Column(format:=Function(Model) Html.ActionLinkWithImage("Action", "Controller", Url.Content("~/Images/printer.png"), "TextoAlternativo", Nothing, Nothing, Nothing), style:="webgrid-width-images"),
grid.Column("ID_CERTIFICADO", "No. Certificado"),
grid.Column("POLIZAS.NUMERO_POLIZA", "No. Poliza"),
grid.Column("PRIMER_APELLIDO_ASEGURADO", "Asegurado", format:=Function(Model) Model.ASEGURADOS.PRIMER_APELLIDO_ASEGURADO & " " & Model.ASEGURADOS.SEGUNDO_APELLIDO_ASEGURADO & " " & Model.ASEGURADOS.NOMBRES_ASEGURADO),
grid.Column("PRIMA_CERTIFICADO", "Prima Total"),
grid.Column("ESTADOS.DESC_ESTADO", "Estado")
)
)
</div>
Make the ActionLinkWithImage helper return MvcHtmlString instead of string. Like this:
<Extension()> _
Public Function ActionLinkWithImage(ByVal helper As HtmlHelper, ByVal actionName As String, ByVal controllerName As String, ByVal imageUrl As String, ByVal alternateText As String, ByVal routeValues As Object, ByVal linkHtmlAttributes As Object, ByVal imageHtmlAttributes As Object) As MvcHtmlString
Dim urlHelper = New UrlHelper(helper.ViewContext.RequestContext)
Dim url = urlHelper.Action(actionName, controllerName, routeValues)
' Create link
Dim linkTagBuilder = New TagBuilder("a")
linkTagBuilder.MergeAttribute("href", url)
linkTagBuilder.MergeAttributes(New RouteValueDictionary(linkHtmlAttributes))
' Create image
Dim imageTagBuilder = New TagBuilder("img")
imageTagBuilder.MergeAttribute("src", urlHelper.Content(imageUrl))
imageTagBuilder.MergeAttribute("alt", urlHelper.Encode(alternateText))
imageTagBuilder.MergeAttributes(New RouteValueDictionary(imageHtmlAttributes))
' Add image to link
linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing)
Return MvcHtmlString.Create(linkTagBuilder.ToString())
End Function

Resources