0

I want to bind column value of database to dropdownlist using storedprocedure in mvc4 i dont know how to handle this .plz help me

my model code is like this

 public class dropdown
{
    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;
    [Required(ErrorMessage = "DomainName is Required")]
    [Display(Name = "DomainName")]
    public string DomainName { get; set; }
    public IEnumerable<dropdown> obj { get; set; }

    public List<dropdown> LoadDomain(dropdown model)
    {
        List<dropdown> obj = new List<dropdown>();
        using (SqlConnection con = new SqlConnection(conn))
        {

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("selectDomainName", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    var data = new dropdown();
                    data.DomainName = ds.Tables[0].Rows[i]["DomainName"].ToString();
                    obj.Add(data);
                }
            }
            catch
            {
            }
        }
        return obj;

in obj i am getting all the column values

my controller code

 public ActionResult binddropdown(dropdown model1)
    {

            var x = model1.LoadDomain(model1);

            ViewData. = x;

            return View(model1);

    }

i dont know wheather i am going in direction or not. i am a beginner so plz suggest me .

1 Answer 1

2

you can set your data in viewbag and then on front side you can loop through data and assign value to your dropdownbox. here what you can do is,

public ActionResult binddropdown(dropdown model1)
{
        var x = model1.LoadDomain(model1);
        viewbag.MyData = x;
        return View();
}

and on front end side you can fetch data from viewbag (conver to appropriate type using typecast if required) and can assign to html tag like like this,

   @{
 var a = (yourobjtypecast) viewbag.MyData ;
<select name="drpId" id="drpId">
      <option value="yourvalue"> Select </option>

                @foreach (var te in a)
                {
                    <option value='@te.ID.ToString()'>@te.Nam </option>

                }
 </select>
}

I hope this work.

Sign up to request clarification or add additional context in comments.

3 Comments

a is just a variable defined above on first line where we have taken our data from viewbag dear. you have to use @{ //coding part } this anotation to work with code on front end along with html
in my dropdownlist i am not getting data
have yo debug ? are you geting data on var "a" ? can you post your code here ? set debug point near var "a" and then check is data coming or not

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.