Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to pass it to another method?
Message
De
12/01/2013 09:34:43
John Baird
Coatesville, Pennsylvanie, États-Unis
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01562213
Message ID:
01562303
Vues:
40
>>>>>Hi everybody,
>>>>>
>>>>>In the top of my code in one method I have this declaration
>>>>>
>>>>>
>>>>>var clients = Db.Clients;
>>>>>
>>>>>I need to pass this object to another method. The method in the prototype application I am looking for has this signature:
>>>>>
>>>>>private JsonResult CreateFlexiJson(IEnumerable<JobPost> items, int page, int total)
>>>>>
>>>>>How can I introduce the same method for my case to be able to pass that clients variable to it?
>>>>>
>>>>>Thanks in advance.
>>>>
>>>>
>>>>private JsonResult CreateFlexiJson(IEnumerable<JobPost> items, int page, int total, ??? clients = null)
>>>
>>>No, I need the clients to become IEnumerable in the declaration. E.g. in the sample application I am working with
>>>http://www.s4sme.com/Blog/Post/aspnet-mvc-4-application-with-flexigrid-jquery-ui-and-jquery-validation
>>>
>>>he is using Jobs (and he uses XML for repository, I guess for simplicity), but in my case my class is Client, but I only have repository available and I access clients by querying Db.Clients. And I need to figure out how to replace the first parameter in that declaration for my case.
>>>
>>>The only solution I was able to implement is
>>>
>>>
>>>private JsonResult CreateFlexiJson(IEnumerable<Client> items, int page, int total)
>>>        {
>>>            return Json(
>>>                    new
>>>                    {
>>>                        page,
>>>                        total,
>>>                        rows =
>>>                            items
>>>                            .Select(x =>
>>>                                new
>>>                                {
>>>                                    id = x.Id,
>>>                                    // either use GetPropertyList(x) method to get all columns 
>>>                                    cell = new { Id = x.Id, Number = x.Number, Name = x.Name, Contact1 = x.Contact1.Contact ?? String.Empty }
>>>                                })
>>>                    }, JsonRequestBehavior.AllowGet);
>>>        }
>>>
>>>E.g. I had to bring Objects reference to that project. Don't know if there are any problems with this approach.
>>
>>Create a class to hold the dbClients properties....Use linq to select from dbClients into an a new instance of you class type then use .ToList() which creates the IEnumerable of your new type. pass that to the function. You really need to learn to think outside of what someone else is doing to solve your problems..
>
>Db.Clients brings me enumerable list of clients, this is Ok. But I don't understand why do I need to create yet another class if I already have Client class. I just didn't want to expose it in the ClientController. I also already have ClientViewModel class which is defined as
>
>
>namespace CardNumbers.Models
>{
>    public class ClientViewModel
>    {
>        public Client Client { get; set; }
>
>        [Key]
>        [Editable(false)]
>        [Column("ClientId", TypeName = "int")]
>        public virtual int? ClientId
>        {
>            get
>            {
>                if (Client == null)
>                    return null;
>                else
>                    return Client.Id;
>            }
>
>            set { Client.Id = value ?? 0; }
>        }
>        [Required]
>        [DisplayName("Client No")]
>        [UIHint("Number")]
>        [Column("client_no", TypeName = "smallint")]
>        [Remote("doesClientNoExist", "Client", HttpMethod = "POST",
>            AdditionalFields = "ClientId",
>            ErrorMessage = "Client Number already exists. Please enter a different Client Number.")]
>        public virtual Int16 Number
>        {
>            get
>            {
>                if (Client == null)
>                    return 0;
>                else
>                    return Client.Number;
>            }
>
>            set { Client.Number = value; }
>        }
>
>        [Required]
>        [Column("client_name", TypeName = "varchar")]
>        [DisplayName("Client Name")]
>        [MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters")]
>        [MinLength(3, ErrorMessage = "Client Name is too short")]
>        [Remote("doesClientNameExist", "Client", HttpMethod = "POST", AdditionalFields = "ClientId",
>            ErrorMessage = "Client Name already exists. Please enter a different Client Name.")]
>        public virtual string Name
>        {
>            get
>            {
>                if (Client == null)
>                    return "";
>                else 
>                    return Client.Name;            
>            }
>
>            set { Client.Name = value; }
>        }
>
>        public int id { get; set; }
>    }
>}
>
>This class I am using in my ClientController already. So, I suspect I can have this signature instead
>
>
> private JsonResult CreateFlexiJson(IEnumerable<ClientViewModel> items, int page, int total)
>
>Or can you clarify what exactly do you mean?


You didn't say in your original post that it was already IENumberable..... If it is, then just pass it to the function. If it is ClientViewModel then pass it like you said in your message.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform