MVC :: How To Convert SelectListItem To Custom Type
Apr 23, 2010
Example: I create a custom type in my domain model (say a "Country" class with Id and Text fields)
A "Person" object have a property of the above type.
The Service class return a list of Countries (IEnumerable<Country> GetCountries();)
Now the ViewModel, derive from the "Person" object and have a member "Countries" which is IEnumerable<SelectListItem> Countries (It convert the Country collection from the service to a collection of SelectListItems.
I use the SelectLIst collection to populate my dropdown.
However when I submit the form, validation fail, obviously because the "Country" dropdown give a string value, while a Country object is expected.
So I need to cast this value from a string (using the "id" of the SelectListItem) to a Country object.
Where and how would be the right way to do this? (Another late night with me bing brain dead and probably overlooking the obvious).
Perhaps should have posted this with my previous question, but now that I figured the problem, it might be better to create a new one.
My viewmodel is a custom type (say Person) and one of it's members another custom type (say "EmploymentStatus").
I got a list of "EmploymentStatuses" which I convert from IEnumerable<EmploymentStatus> to a SelectList
[Code]....
and I use this list in my View (Strongly typed using PersonModel)
[Code]....
All fine. The problem is that my controller expect a person object and when it does validation, it tell me that the employment status selection are not valid. Obviously because a EmploymentStatus object are expected whereas the View return a SelectListItem.
I am producing a List(Of SelectListItem) from my database and passing this to a DropDownList Html Helper method on my View via the controller.This works ok but I seem to have found a bug or issue where the DropDownList ignores the Selected property of the SelectListItem objects in the list.I have a provider class in my model which will read the database, create the List(Of SelectListItem) and then depending on whether or not the user is associated with one of the options, I will set the Selected property to True for that item.
However, when I try to build the control I'm getting the error as shown in the subject. I've tried searching, and it seems the signature for the original function matches the one I have, and all other solutions I've seen uses the same signature.
how to make a item the default from a dropdownlist, I want to make the value GBP as the option selected when the page loads, i've spent all day trying to get ddlist to work and now i'm stuck on this.
this
.ViewData["ddlCurrency"] = new SelectList(ws.ToList(), "dboCurrencyID", "dboCountry", new SelectListItem { Selected = true, Text = "United Kingdom (GBP)", Value = "GBP" });
I have created a user control and am ready to convert it into a custom control. I am trying to follow:
[URL] the steps mentioned in the link.
I am not seeing Build / Publish Web Site option.
I am using VS 2008. I only see Publish User Control but it doesn't give all the settings mentioned below. When I do publish user control, it just copies the ascx file to the desired location. how can I convert the user control to custom control.
So, I've been programming for a number of years. Finding some intricately asinine issues w/ ASP.NET and VB.NET as I learn them. How about this one:
[Code]....
The issue is not Date1 The issue is Date2.
No kidding. I had to String.Format Date1 for some odd reason, even though I'd already validated it. And, yes, I tried to parse and convert Date1 but I kept gettting the typical "Argument 'Date2' cannot be converted to type 'Date'." error. (Did you catch the copy & paste? I didn't edit it from my existing error, but Date2 should be Date1.) Yeah, that's the error I'm getting now with the above ...
"Argument 'Date2' cannot be converted to type 'Date'."
Date2 is a system defined type. Why can't the system convert and/or simply use it's own pre-defined type?
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string' OK I am fairly new to LINQ, but I am starting to get the hang of it. So here is the of my code that bugs:
[Code]....
And it returns this error, on the second line for p:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string'
Anybody know how to start trying to figure this error out? This is the only error I get when debugging the app. I get no errors during the compile phase. This is an ASP.NET 3.5 App. Breaking on my local machine and a server.
Line 149: public new ASP.masterpage_master Master { Line 150: get { Line 151: return ((ASP.masterpage_master)(base.Master)); Line 152: } Line 153: } Source File: c:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files oote04eaf87eb3a5588App_Web_tmdefault.aspx.cdcab7d2.ytin7a8n.0.cs Line: 151
On the Server I get this error:
Parser Error, Description: An error occurred during the parsing of a resource required to service this request. review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'Default'.
Source Error:
Line 1: <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Default" Debug="true"%> Line 2: <%@ MasterType virtualPath="~/MasterPage.master"%> Line 3:
I am looking to convert a String (which contain my XML) to Clob type in VB.net 2005 and call a Oracle Store Procedure which return me XML in Clob type and need Convert it back to String.
Please consider below example for better reference..
Below Method Call the Oracle Procedure with String as Input
Private Function Pricing(ByVal sXML As String, ByVal strOutputXML As String, ByVal sStatus As String, ByVal sMsg As String)
Dim objParams(3) As OracleParameter objParams(0) = New OracleParameter("input_xml", OracleType.Clob, 100000) objParams(0).Direction = ParameterDirection.Input objParams(0).Value = myclob objParams(1) = New OracleParameter("output_xml", OracleType.Clob, 100000) objParams(1).Direction = ParameterDirection.Output objParams(2) = New OracleParameter("status", OracleType.VarChar, 100) objParams(2).Direction = ParameterDirection.Output objParams(3) = New OracleParameter("msg", OracleType.VarChar, 1000) objParams(3).Direction = ParameterDirection.Output ExecuteDataset(objOMARConnection, CommandType.StoredProcedure, "PRICING_INT.ITEM_PRICE", objParams)
End Function
ORACLE Procedure
PROCEDURE item_price ( input_xml IN CLOB, output_xml OUT CLOB, status OUT VARCHAR2, msg OUT VARCHAR2
I want to log all errors happened in my App to sql db. I'm passing all exception properties like (ex.Message, ex.Source, ex.StackTrace, ex.TargetSite and ex.InnerException) to stored procedure.
The problem is that when I try to pass the ex.InnerException as string to stored procedure I got this error:
Cannot implicitly convert type 'System.Exception' to 'string'
I tried ex.InnerException.ToString() and Convert.ToString (ex.InnerException) but no success.
I am trying to get text box values into a database from a asp.net form. In my SQL server database I have defined the field emailID as nvarchar(50) and I still get the error messageconversion failed when converting the varchar value abcd@efg.com to type int" I am not trying to convert it int so my guess is SQL server is doing some kind of implicit conversion.
I have been looking for a way to convert C# datatype into database specific type of MS SQL. For example when I do [Code]....
I would get System.GUID or something like that but what I want is UniqueIdentifier instead of System.GUID. In most cases, I would also want the length of the type like varchar(1000) and stuff like that. Is there anyway I could convert or get similiar result ?