使用 Html.DropDownListFor<>在 asp.net mvc 2 中

发布于 2024-10-25 06:55:35 字数 1013 浏览 1 评论 0 原文

我在控制器中有以下数据

private void GetCountryLists(string value)
    {
        List<country> countries = new List<country>();
        country _country = new country() { countryname = "Select a country", value = "0" };
        country _country1 = new country() { countryname = "India", value = "India" };
        country _country2 = new country() { countryname = "USA", value = "USA" };
        countries.Add(_country);
        countries.Add(_country1);
        countries.Add(_country2);

        ViewData["country"] = new SelectList(countries, "value", "countryname", value);
    }

国家/地区类如下,

public class country
{
    public string countryname { get; set; }
    public string value { get; set; }
}

现在,我如何使用 Html.DropDownListFor<> 从视图页面使用 ViewData["country"] 数据;() 显示国家/地区列表。在视图页面中,我有 Model 属性,可以从中获取用户的国家/地区值。我必须在下拉列表中显示用户国家/地区,并选择当前用户所在的国家/地区。

此代码用于编辑页面。

请建议我一个简单的方法,以便我作为新手开发人员可以学习和使用。

I have the following data in the controller

private void GetCountryLists(string value)
    {
        List<country> countries = new List<country>();
        country _country = new country() { countryname = "Select a country", value = "0" };
        country _country1 = new country() { countryname = "India", value = "India" };
        country _country2 = new country() { countryname = "USA", value = "USA" };
        countries.Add(_country);
        countries.Add(_country1);
        countries.Add(_country2);

        ViewData["country"] = new SelectList(countries, "value", "countryname", value);
    }

The country class is as follows,

public class country
{
    public string countryname { get; set; }
    public string value { get; set; }
}

Now, how do i use the ViewData["country"] data from the view page using the Html.DropDownListFor<>() to display the country list. In the viewpage, i have the Model property from which i can get the user's country value. I have to display the user country in the dropdown list with the selection of the current user's country.

This code is for the edit page.

Kindly suggest me a simple approach so that i can learn and use as i am a newbie developer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-11-01 06:55:35

这是一种方法(不推荐,因为 ViewData,您可以直接跳到第二个建议):

假设您有以下控制器操作:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.SelectedCountryValue = "India";
    GetCountryLists(model.SelectedCountryValue);
    return View(model);
}

并且在视图中:

<%= Html.DropDownListFor(
    x => x.SelectedCountryValue, 
    (SelectList)ViewData["country"]
) %>

现在推荐的方法:

模型:

public class MyViewModel
{
    public string SelectedCountryValue { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }
}

控制器:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        SelectedCountryValue = "India",
        Countries = GetCountries()
    };
    return View(model);
}

private IEnumerable<SelectListItem> GetCountries()
{
    return new[]
    {
        new SelectListItem { Value = "India", Text = "India" },
        new SelectListItem { Value = "USA", Text = "USA" },
    }
}

并在视图中:

<%= Html.DropDownListFor(
    x => x.SelectedCountryValue, 
    new SelectList(Model.Countries, "Value", "Text"),
    "Select a country"
) %>

Here's one way (not recommended because of ViewData, you directly may skip to the second suggestion):

Assuming you have the following controller action:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.SelectedCountryValue = "India";
    GetCountryLists(model.SelectedCountryValue);
    return View(model);
}

and in the view:

<%= Html.DropDownListFor(
    x => x.SelectedCountryValue, 
    (SelectList)ViewData["country"]
) %>

And now the recommended way:

Model:

public class MyViewModel
{
    public string SelectedCountryValue { get; set; }
    public IEnumerable<SelectListItem> Countries { get; set; }
}

Controller:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        SelectedCountryValue = "India",
        Countries = GetCountries()
    };
    return View(model);
}

private IEnumerable<SelectListItem> GetCountries()
{
    return new[]
    {
        new SelectListItem { Value = "India", Text = "India" },
        new SelectListItem { Value = "USA", Text = "USA" },
    }
}

and in the view:

<%= Html.DropDownListFor(
    x => x.SelectedCountryValue, 
    new SelectList(Model.Countries, "Value", "Text"),
    "Select a country"
) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文