2

I have the below example html select (dropdown)

<select name="Testing">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4" selected>Select</option>
</select>

And the following SQL Server Table:

ID NAME
1  Apple
2  Orange
3  Peach

This drop down is part of a form which is posted to server, the value is read using:

reqparm.Add("TestDropdown", Request["Testing"].ToString());

How do I populate the drop down while keeping the name attribute?

4
  • what have u tried so far? Commented Sep 17, 2014 at 10:58
  • itorian.com/2013/02/… Commented Sep 17, 2014 at 11:00
  • Thanks for the responce, I haven't tried any solution yet Arijit as i dont have one to test yet, but I don't think I can use dropdownlist or dropdownlistfor because they have no name property which would break the http post to server. Commented Sep 17, 2014 at 11:12
  • Dropdownlist have Text and Value property Text will be treated as Name and Id as Value Commented Sep 17, 2014 at 11:32

1 Answer 1

2

In the end the solution I produced avoided the html helpers (personally don't like them)

In your index method of home controller:

// create list of my viewmodel policyorg equals my db table in a list

List<PolicyOrganisation> policyOrgs = db.PolicyOrganisations.ToList();

// pass results to viewbag

ViewBag.PolicyOrgs = policyOrgs;

then in Index.cshtml

// Loop throught items in viewbag and add each to the select

<select id="DD1" name="PolicyOrganisation">
                        <option value="-1">Select</option>    
                        @foreach (var item in ViewBag.PolicyOrgs)
                        {
                            <option value="@item.Id">@item.Name</option>
                        }
                    </select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.