View
<div class="editor-label">
Country
</div>
<div class="editor-field">
@Html.DropDownListFor(model=>model.CountryId, new SelectList((ViewBag.Countries) as SelectList, "Value", "Text"), "Select")
</div>
-----------------------------------
Controller
CountryDA countryda = new CountryDA ();
ViewBag.Countries =new SelectList(countryda.ReadAllCountry(), "CountryId", "CountryName", _country.CountryId);
-------------------------------
DATA Access
public List<Country> ReadAllCountry()
{
List<Country> country = new List<Country>();
try
{
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand com = new SqlCommand("Usp_ReadAllCountry", con);
com.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
country.Add(new Country { CountryId = Convert.ToInt32(dr["CountryId"]), CountryName = Convert.ToString(dr["CountryName"]) });
}
}
}
catch
{
throw;
}
return country;
}
---------------------------------------
Model
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
<div class="editor-label">
Country
</div>
<div class="editor-field">
@Html.DropDownListFor(model=>model.CountryId, new SelectList((ViewBag.Countries) as SelectList, "Value", "Text"), "Select")
</div>
-----------------------------------
Controller
CountryDA countryda = new CountryDA ();
ViewBag.Countries =new SelectList(countryda.ReadAllCountry(), "CountryId", "CountryName", _country.CountryId);
-------------------------------
DATA Access
public List<Country> ReadAllCountry()
{
List<Country> country = new List<Country>();
try
{
using (SqlConnection con = new SqlConnection(connectionstring))
{
SqlCommand com = new SqlCommand("Usp_ReadAllCountry", con);
com.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
country.Add(new Country { CountryId = Convert.ToInt32(dr["CountryId"]), CountryName = Convert.ToString(dr["CountryName"]) });
}
}
}
catch
{
throw;
}
return country;
}
---------------------------------------
Model
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
No comments:
Post a Comment