Environment ASP.net MVC5+ entity framework code-first
环境ASP.net MVC5+实体框架代码优先
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
对于VenderCityId,我想要另一个级别的“City”,它有ID和名称
I have a VenderViewModel
我有一个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.
通过调试,我可以确认这个viewmodel具有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 个解决方案
#1
2
You need to select the Id within CityItems
by adding this:
您需要在CityItems中选择Id,添加以下内容:
Selected = VenderID.Equals(x.Key)
When you populate your SelectList do this:
当您填充SelectList时,请这样做:
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助手改为:
@Html.DropDownListFor(model => model.Vender.VenderCityId, Model.CityItems, "Please select ")
This will make use of strongly-typed objects rather than using the ViewBag
这将使用强类型对象,而不是使用ViewBag
#1
2
You need to select the Id within CityItems
by adding this:
您需要在CityItems中选择Id,添加以下内容:
Selected = VenderID.Equals(x.Key)
When you populate your SelectList do this:
当您填充SelectList时,请这样做:
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助手改为:
@Html.DropDownListFor(model => model.Vender.VenderCityId, Model.CityItems, "Please select ")
This will make use of strongly-typed objects rather than using the ViewBag
这将使用强类型对象,而不是使用ViewBag