0

I have a model like

  [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }

    public IEnumerable<Connection> Connections { get; set; }

And Connection class is

 public class Connection
{

    public int ConnectionId { get; set; }
    public string Name { get; set; }
}

I want to select a database from a drop-down list which will have a user name and password.

In my Controller I have Created Connection list like below

public ActionResult Create()
    {
        var databaseConnection = new DatabaseConnection();

        databaseConnection.Connections = new List<Connection>
            {
                new Connection()
                    {
                        ConnectionId = 1,
                        Name = @"10.44.171.39\SQL2K8R2"
                    },
                new Connection()
                    {
                        ConnectionId = 2,
                        Name = "TestDb"
                    }
            };

        return View(databaseConnection);
    }

And the corresponding view is

<div>
        <span>Select Database</span>
        <p>
            <select name="Section">
                <option value="" selected="selected">Select Section</option>
                @foreach (var item in Model.Connections)
                {
                    <option value="@item.ConnectionId">@item.Name</option>
                }
            </select>
        </p>
    </div>

When I am posting the form I am getting username and password Ok but not getting the

Connection Name and Id .

Any help would be appreciated Thanks in advance :)

1
  • Doing it the way you have, your select would be named 'ConnectionId' and the name will not come over, only the value for ConnectionId. Even in the solution below that is the case, you will need to look the name up server side or store it when selected into a hidden field. Commented Feb 28, 2013 at 6:38

1 Answer 1

1

Modify your <select> like this

<div>
        <span>Select Database</span>
        <p>
            @Html.DropDownListFor(
                m => m.Connections.ConnectionId,
                new SelectList(Model.Connections, "ConnectionId", "Name"),   
                "Select a Connection"
            )
        </p>
</div>

When you have posted your form, then it will automatically bind to the ConnectionId property.

Now add a post action to your controller.

[HttpPost]
public ActionResult Create(DatabaseConnection connection)
{
    //get the selected value from dropdown.
    var selected=connection.Connections.ConnectionId;
    //do other stuff..
    return View();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok I had to add public Connection Connection { get; set; } to my DatabaseConnection class Thanks for your reply @Karthik

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.