1

I want to retrieve data from db into a model and pass it to the view

e.g. 1) db State table, has 2 columns Code and Text. 2) my model:

public class state 
{
    public IEnumerable<SelectListItem> list { get; set; }
    public string selected { get; set; } 
}

3) and I want the controller to place code and text data from state table into list and set selected to 1 and return the model to the view

how would I write the loop in the controller

1 Answer 1

2
public class state
{
    public List<SelectListItem> list { get; set; }
    public string selected { get; set; }

    public state()
    {
        SqlConnection DbConn = new SqlConnection(YourConnectionStringHere);
        SqlCommand SelectStates = new SqlCommand();
        SelectStates.CommandText = "select code from state";
        SelectStates.Connection = DbConn;

        DbConn.Open()
        SqlDataReader ReadStates = SelectStates.ExecuteReader();

        while (reader.Read())
            list.Add(
                new SelectListItem() { Text = reader[0].ToString(), Value = reader[0].ToString() });

        DbConn.Close();

        list[0].Selected = true;
    }
}

Ideally, you could create a data access class to do what that above constructor does, but you should get the general idea of the code.

As for the Action method:

public ActionResult StateSelect()
{
    state YourViewModel = new state();

    return View(YourViewModel);
}

A corresponding View:

@model state

@* Add your other markup here *@

@Html.ListBoxFor(m => m.selected, Model.list)
Sign up to request clarification or add additional context in comments.

4 Comments

thx so much for the code, Do you think it would be better to create a model containing Code and Text fields instead of what I have. and in the controller generate the list and forward it to the view?.... Also, if I wanted to use linq to Entity instead of sql how would you modify the constructor, I just want to be aware of other opts! Thanks
@Ben in my opinion, the Controller shouldn't be creating the list. That should all be handled by your view model. As for the model creation, what data do you need for the domain? Also, typically SelectList is for ViewModels (as they pertain to Views), so maybe you should split that out as well.
let me explain what I am trying to do, Currently I have a State table in the database and I wanted to show the data in the listbox/ddl and I wasn't sure if I should create the model like the database table or have my model contain the list. Also wanted to try to do it using EF. I hope it makes sense what I want to do.
@Ben the real question is, does your model (and indirectly, your domain/View) need all of the database table? Don't push data to the Business and Presentation tiers that they don't need. As for EF, I'm not the best person to ask about that. As I typically use direct ADO.NET and the data access like I've shown in my answer. That EF question should be a new, separate question. Make sense?

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.