DropDownList的行为并不如预期 [英] DropDownList not behaving as expected

查看:139
本文介绍了DropDownList的行为并不如预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的DropDownListFors说我希望你能帮助我的麻烦。我猜这是那些东西,你要么知道一你不知道。

I'm having trouble with my DropDownListFors that I'm hoping you can help me with. I'm guessing it's one of those things that you either know or you don't.

问题是我有一个国家的表在我的数据库有它的国家名单。我会从我的降下来一样的行​​为是建立在我的地址表指向在下拉选择下降县的外键引用。我得到的行为是,在我的地址表的外键指向在这是完全不必要的国家一个新的条目。

The problem is I have a Countries table in my database which has a list of countries in it. The behaviour I would like from my drop down is to create a foreign key reference in my Address table pointing to the county selected in the drop down. The behaviour I'm getting is that the foreign key in my Address table is pointing to a new entry in the Country which is totally unwanted.

谁能解释如何做到这一点?我不知道什么code你们希望看到的,所以请让我知道如果你能帮助。

Can anyone explain how to do this? I'm not sure what code you guys would like to see so please let me know if you can help.

===更多信息===

=== More information ===

好吧,我有这样的视图模型类:

Ok, I have a view model class like this:

public class CountryViewModel
{
    public int CountryId { get; set; }
    public string Name { get; set; }
}

在我看来,我有一个下拉列表是这样的:

and in my view I have a drop down list like this:

@Html.DropDownListFor(m => m.LegalEntity.Address.Country.CountryId,
    new SelectList( Model.LegalEntity.Address.Country, 
        "CountryId", "Name", Model.LegalEntity.Address.Country.CountryId),
new { @class = "form-control" })              

请注意,这个第二行目前没有工作,我不知道如何让国家的整个列表到这个

Note that the second line of this does not currently work: I don't know how to get the entire list of countries into this.

我的法律实体视图模型是这样的:

My legal entity view model looks like this:

public class LegalEntityViewModel
{
    [Key]
    public int LegalEntityID { get; set; }
    public virtual AddressViewModel Address { get; set; }
    public virtual TechnicalContactViewModel TechnicalContact { get; set; }
}

和我的地址视图模型看起来是这样的:

and my address view model looks like this:

public class AddressViewModel
{
    [Key]
    public int AddressID { get; set; }
    ...
    [Display(Name = "Country")]
    public virtual CountryViewModel Country { get; set; }
}

我想的行为是对所有国家来填充下拉菜单和选定的国家在我LegalEntityViewModel.AddressViewModel.CountryViewModel结束。

The behaviour I would like is for all the countries to populate the drop down and the selected country to end in my LegalEntityViewModel.AddressViewModel.CountryViewModel.

帮助!我一直在摆弄这个和重构一整天!

Help! I've been fiddling with this and refactoring all day!

期待您的回复。

M

推荐答案

有多种方法可以做到这一点。例如,你可以加载你的国家名单AddressViewModel。

There's multiple ways to do this. For example you could load the list of countries in you AddressViewModel.

即。

public class AddressViewModel
{

    [Display(Name = "Country")]
    public int SelectedCountryId { get; set; }

    public IEnumerable<SelectListItem> Countries { get; set; }
}

然后在你认为这样做

Then in your view do this

@Html.DropDownListFor(m => m.SelectedCountryId , new SelectList(Model.Countries , "Value", "Text"))

您也可以加载你的国家名单使用Javascript。

You could also load you Countries list with Javascript.

$(document).ready(function () {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("GetCountries")',  <--This will be a method in your controller that brings back the Countries,
        success: function (results) {
        var options = $('#SelectedCountryId');
        $.each(results, function () {
            options.append($('<option />').val(this.CountryId).text(this.CountryName));
        });
    }
    });

  public class CountryViewModel
  { 
       public int CountryId {get;set;}
       public int CountryName {get;set;
  }

在控制器

    [HttpPost]
    public JsonResult GetCountries()
    {
        var countries = //some method to get the countries for a database or something
        var countriesList = countries .Select(x => new CountryViewModel { CountryId  = x.CountryId, CountryName = x.CountryName }).ToList();
        return this.Json(countriesList );
    }

这篇关于DropDownList的行为并不如预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆