简体   繁体   中英

ASP.NET MVC dropdownlist not selecting value

Environment ASP.net MVC5+ entity framework code-first

public class Vender
{
    public int VenderID { get; set; }


    public string VenderCode { get; set; }


    public string CompanyCode { get; set; }


    public string CompanyFullName { get; set; }


    public string CreateUser { get; set; }


    public int VenderCityId { get; set; }

}

For VenderCityId like to another Class "City", it has the ID and Name

I have a VenderViewModel

public class VenderViewModel
{

    public Vender Vender;

    public VenderViewModel(IVenderRepository _venderRepository,int? Id)
    {
        if (Id==null)
        {
            Vender = new Vender();
        }
        else
        {
            Init(_venderRepository, Id);
        }
    }

    private void Init(IVenderRepository _venderRepository, int? Id)
    {
        Vender = _venderRepository.GetVenderById(Id);

    }


    public IEnumerable<SelectListItem> CityItems
    {
        get
        {
            IEnumRepository cityEnumRepository=new EnumRepository(new MasterDataContext());
            IEnumerable<City> _city = cityEnumRepository.GetAll<City>();

            var allCitys = _city.Select(m => new SelectListItem
            {
                Value = m.Id.ToString(),
                Text = m.Name

            });
            //return allCitys;
            return DefaultCity.Concat(allCitys);
        }
    }

    public IEnumerable<SelectListItem> DefaultCity
    {
        get
        {
            return Enumerable.Repeat(new SelectListItem
            {
                Value = "-1",
                Text = "Select a City"
            }, count: 1);
        }
    }

Here is about the controller, just simple

public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        //Vender vender = _venderRepository.GetVenderById(id);
        VenderViewModel vm = new VenderViewModel(_venderRepository, id);
        if (vm.Vender == null)
        {
            return HttpNotFound();
        }
        return View(vm);
    }

From the debug, I can confirmed that this viewmodel has the value for VenderCityId.

At last the view:

 @Html.DropDownListFor(model => model.Vender.VenderCityId, Model.CityItems)

the question is that dropdownlist can not show the correct item. no item is select after loading.

You need to select the Id within CityItems by adding this:

 Selected = VenderID.Equals(x.Key)

When you populate your SelectList do this:

var allCitys = _city.Select(m => new SelectListItem
            {
                Value = m.Id.ToString(),
                Text = m.Name,
                Selected = VenderID.Equals(m.Id.ToString())

            });

Then change your Html helper to this:

@Html.DropDownListFor(model => model.Vender.VenderCityId, Model.CityItems, "Please select ")

This will make use of strongly-typed objects rather than using the ViewBag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM