3

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.

1
  • I just injecting the repository. It can not resolve this Commented Nov 23, 2013 at 15:16

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I don't catch it? why do you think add a optionlabel will fix this? I just try it, It doesn't work
Sorry, I couldn't test this before as I wasn't on a pc. Please try the above. The important bit is where you set the selected value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.