asp.net mvc - how to bind column value from database to html dropdownlist in mvc4 using ado.net -
I want to bind the database value of the database to dropdown using the stored procedure in mvc4, how do I handle it I do not know. Plz me
My model code is such that
public class dropdown {string conn = System.Configuration.ConfigurationManager.ConnectionStrings ["cnstr"]. ConnectionString; [Required (error message = "domain name is required"]] [display (name = "domain name")] Public string domain name {get; Set; } Public IEnumerable & lt; Dropdown & gt; Obj {get; Set; } Public listing & lt; Dropdown & gt; Loaddown (dropdown model) {list & lt; Dropdown & gt; Obj = new list & lt; Dropdown & gt; (); (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 & lt; = ds.Tables [0] .Rows.Count - 1; i ++) {var data = new dropdown (); Data.DomainName = ds.Tables [0] .Rows [i] ["DomainName"]. ToString (); Obj.Add (data); }} Hold {}} return obj; obj I get all the column values My Controller Code
Public Action (Dropdown model 1) {var x = model1.LoadDomain (model1); ViewData = X; See Return (Model 1); } I do not know whether I am going in the direction or not. I am a beginner, so I suggest that.
You can set your data in viewbags and then loop through the data in front of you. And can assign values to your dropdown box. Here's what you can do,
Public Action Binddropdown (dropdown model 1) {var x = model1.LoadDomain (model1); Viewgag.maydata = x; See Return (); } And on the front edge, you can get data from ViewBug (as per the requirement of proper type converter according to the type of cardcard) and thus assign the html tag,
@ {var a = (yourobjtypecast) viewbag.MyData; & Lt; Select name = "drpId" id = "drpId" & gt; & Lt; Option value = "your price" & gt; & Lt; / Options & gt; Choose @Archach (wer te) {& lt; Option value = '@ to ID ToString () '& gt; @ Te. NAM & lt; / Options & gt; } & Lt; / Select & gt; } I hope this work.
Comments
Post a Comment