开发者

ASP.net MVC create model with value with multiple options

开发者 https://www.devze.com 2023-03-12 16:03 出处:网络
I am wanting to 开发者_如何学Ccreate a Model that has a property of Service which then has multiple options that can either be true or false. So my Contact Class:

I am wanting to 开发者_如何学Ccreate a Model that has a property of Service which then has multiple options that can either be true or false. So my Contact Class:

public class Contact
{
    //My Properties
}

I want the Contact class to have a property of Services. I have a list of services available. How can I reference my services from my Contact class. I would like be able to access it in my view like: model.services.MyCustomService if it's even possible. I come from a javascript background so this is what I mean but written in javascript if it helps.

Javascript Example

var Contact = {
    property: "",
    services: {
        MyCustomService: "",
        MyCustomService2: "",
    }
}


Here's how I'd do it:

In your Model:

public class Contact
{
    //My Properties

    public List<Service> Services
    {
        get;
        set;
    }
}

In your Controller:

public ActionResult Index()
{
    var model = new Contact(); // Create model

    // Create some sample services
    var service = new Service();
    var service2 = new Service();

    // Add the services
    model.Services.Add(service);
    model.Services.Add(service2);

    // Pass the model to the view
    return View(model);
}

Then, in your view:

@model MySite.Models.Contact

@foreach (var service in Model.Services)
{
    <text>Here's my service: @service.MyCoolProperty.ToString()</text>
}

In that simple example, I first declared the Services property of your Contact class as a List<Service>. That allows you to combine many Services into one property - perfect for passing along to a view.

Next, in the Controller, I added some Services to the List by using the Add() method. Therefore, in the View, you're now able to access those Services through Model.Services.

To me, that looks like one of the simplest ways to approach this common problem. Hope that helped!


In C#, every object has a type so Services property has to have an actual type if you want to reference MyCustomService and MyCustomService2 through dot notation:

class Contact {
    public ServiceContainer Services { get; set; }
}

class ServiceContainer {
    public Service1 Service1 { get; set; }
    public Service2 Service2 { get; set; }
}

However, if container serves for no other purpose but to store a service (assuming it's some object), you should probably store them in a list or array (access by index) or in a dictionary (access by string).

To give you more information, I'll need to know what exactly these services are, whether you expect their set to change, do they have different types, etc.


To bind to a collection all you need is this:

<%@ Page Inherits="ViewPage<IList<Book>>" %>

A complete explanation can be found here:

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消