John John
John John

Reputation: 1

How can I store more than 2 values inside a SelectListItem

I am working on two different databases , which store IP addresses, so I am populating a dropdown which shows the IP address from two database as follow:-

public JsonResult LoadRelatedIPs(string searchterm, string Searchby)
        {

            var ITsysips = repository.getTechnologyIPs(searchterm, Searchby).Select(a => a.IPAddress).ToList();


            var it360ips = repository.getIT360TechnologyIPs(searchterm, Searchby).Select(a => a.IPADDRESS).ToList();

            var join = ITsysips.Union(it360ips).ToList();
            var CSData = join.Select(m => new SelectListItem()
            {
                Text = m,
                Value = m,

            });

            return Json(CSData.OrderBy(a => a.Text), JsonRequestBehavior.AllowGet);


        }

But the problem I am facing is that when I post the value back to my controller I am unable to determine the location of the IP, and to do so I need to query the two databases to know the source of the IP address, my model looks as follow:-

 public partial class ITsysSwitchPort
    {
        public int TechnologyID { get; set; }
        public int SwitchID { get; set; }
        public string PortNumber { get; set; }
        public Nullable<int> ITsysTechnologyIPID { get; set; }
        public Nullable<long> IT360NetworkID { get; set; }

        public virtual Technology Technology { get; set; }
        public virtual TechnologyIP TechnologyIP { get; set; }
        public virtual ITsysSwitch ITsysSwitch { get; set; }


  }

where the ITsysTechnologyIPID & IT360NetworkID stores the ID of the IP address from the 2 databses.(of course for certain record one of the values will be null) So my question is wheatear I can pass additional values inside my SelectListitem, containg the ITsysTechnologyIPID & IT360NetworkID ? So that I do not need to query the DB to re-check the ip location ? currently inside my view displaying the IP address dropdownlist using a field named generalIP as follow, which get populated on runtime using jQuery:-

 <span class="f">Site @Html.DisplayNameFor(model=>model.GeneralIP)</span>
        @Html.DropDownListFor(model => model.GeneralIP, Enumerable.Empty<SelectListItem>())
  @Html.ValidationMessageFor(model => model.GeneralIP) 

Thanks

Upvotes: 0

Views: 1245

Answers (3)

Dzoukr
Dzoukr

Reputation: 1145

What about to "mark" data (value of option) with source prefix?

var CSData = join.Select(m => new SelectListItem()
{
    Text = m,
    Value = String.Format("{0}_{1}", ITsysips.Contains(m) ? "SysIPs" : "360IPs" ,m),
});

Then when working with selected value from option you can check like:

if(valueFromOption.StartsWith("SysIPs"))
{
// it is from getTechnologyIPs method
}
else
{
// etc...
}

Upvotes: 1

mnsr
mnsr

Reputation: 12437

One way to do this is join the string into the Value so that you have the ID and IP.

e.g. id+"-"+IP

Then when you post to controller, split the string from the - and you'll have the ID and IP address.

But as Nicholas Carey said, SelectListItem = <option> which can only have text & value.

Upvotes: 1

Nicholas Carey
Nicholas Carey

Reputation: 74187

The <option> element has pretty much this syntax:

<option value="value-of-option"
        label="display-text-of-option"
>
  option-content
</option>

The display text for the option is either the content of the <option> element or the value of its label attribute. The label attribute is used in preference to the content.

If no value attribute is present, the value of the option (what gets submitted with the form) is the display text. If the value attribute is present, its value is submitted with the form.

You can put whatever you want into the value attribute: if you wanted to encode a serialized and encrypted representation of an entire object instance as the value of an <option>, you could [theoretically] do so.

When the form is submitted to the server, the meaning and interpretation of that value is entirely up to the receiving app.

So, yes, you can do what you ask. You might want to consider the security implications of sending internal data down to the client. Encrypting the option values might suggested so as to prevent user agents from mucking about with it.

Upvotes: 2

Related Questions