问题描述
我正在努力找出如何将我的模型(未修改)传回给我的控制器。我已经尝试绑定我的ViewModel许多不同的HTML.Helpers和什么不是。我可以得到我的数据绑定和显示,但是我不能让ViewModel发回到我的控制器。
I am struggling to figure out how to pass back my model (unmodified) back to my controller. I have tried binding my ViewModel to many different HTML.Helpers and what not. I can get my data to bind and display, but I cannot get the ViewModel to post back to my controller.
我有以下ViewModels:
I have the following ViewModels:
public class MediaProviderViewModel : ViewModelBase
{
public int MediaProviderId { get; set; }
public string MediaProviderName { get; set; }
public static MediaProviderViewModel FromEntity(MediaProvider entity)
{
if (entity == null)
{
return (null);
}
return (new MediaProviderViewModel()
{
MediaProviderId = entity.MediaProviderId,
MediaProviderName = entity.Name,
});
}
public static IEnumerable<MediaProviderViewModel> FromEntityList(IEnumerable<MediaProvider> entities)
{
return entities == null ? null : entities.Select(FromEntity).OrderBy(mp => mp.MediaProviderName);
}
}
public class SystemConfigViewModel : ViewModelBase
{
[DisplayName("Media Providers")]
public IEnumerable<MediaProviderViewModel> MediaProviders { get; set; }
public SystemConfigViewModel()
{
}
public static SystemConfigViewModel FromEntity(Entities db)
{
return new SystemConfigViewModel()
{
MediaProviders = MediaProviderViewModel.FromEntityList(db.MediaProviders)
};
}
}
控制器:
public class SystemConfigController : CommonBaseController
{
//
// GET: /SystemConfig/
public ActionResult Index()
{
SystemConfigViewModel model = SystemConfigViewModel.FromEntity(_db);
return View(model);
}
[HttpPost]
public ActionResult Index(SystemConfigViewModel model)
{
return View(model);
}
查看:
<html>
<body>
@using (Html.BeginForm("Index", "SystemConfig"))
{
@Html.ListBoxFor(model => model.MediaProviders, new SelectList(Model.MediaProviders, "MediaProviderId", "MediaProviderName"), new { @size = "30" })
}
</body>
</html>
我已经尝试过遍历项目并将其编入索引:
I have tried iterating through the items and indexing them like such:
//在一个for循环中用不同的帮助器包围
//Surrounded in a for loop with different helpers
model => model.MediaProviders[i].MediaProviderId
仍然没有回传。我可以帮助我做错什么吗?我尝试了谷歌搜索,但没有什么真正的价值弹出我的情况。
Still nothing is passed back. Could I get some help as to what I am doing wrong? I tried googling but nothing really of value popped up for my case.
推荐答案
您应该在视图模型中添加一个附加属性,将所选值绑定到(并更改 MediaProviders
到 SelectList
也会更容易)
You should include an additional property in you view model to bind the selected values to (and changing the MediaProviders
to a SelectList
would also make it easier)
public class SystemConfigViewModel : ViewModelBase
{
public IEnumerable<int> SelectedProviders { get; set; } //bind to this
public SelectList MediaProviders { get; set; } // populate this in the controller
}
然后在视图中
@Html.ListBoxFor(m=> m.SelectedProviders , Model.MediaProviders, new { @size = "30" })
发回时, SelectedProviders
属性将包含所选项目的ID( MediaProviderId
)。
When you post back, the SelectedProviders
property will contain the ID's (MediaProviderId
) of the selected items.
这篇关于使用ViewModel填充ListBox并传回其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!