我目前正在为我的项目开发一个编辑功能,我似乎无法让视图模型正确地允许它从视图传递回控制器。View Model 的结构是这样的:
public class CreateUserViewModel : ICreateUserViewModel
{
#region Properties
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string SelectedUserType { get; set; }
public List<ICreateUserItemViewModel> UserTypes { get; set; }
public List<ICreateUserItemViewModel> Products { get; set; }
public List<ICreateUserItemViewModel> Languages { get; set; }
#endregion
#region Constructor
public CreateUserViewModel()
{
}
public CreateUserViewModel(List<Product> products, List<Language> languages)
{
Products = new List<ICreateUserItemViewModel>();
foreach (var prod in products)
{
var prodVM = new CreateUserItemViewModel
{
Name = prod.Name,
IsSelected = false,
ID = (int)prod.ID
};
Products.Add(prodVM);
}
Languages = new List<ICreateUserItemViewModel>();
foreach (var lang in languages)
{
var langVM = new CreateUserItemViewModel
{
Name = lang.Name,
IsSelected = false,
ID = (int)lang.ID
};
Languages.Add(langVM);
}
}
#endregion
}
子类 ViewModel CreateUserItemViewModel
:
public class CreateUserItemViewModel : ICreateUserItemViewModel
{
public string Name { get; set; }
public int ID { get; set; }
public bool IsSelected { get; set; }
}
我希望这个子类在视图中表示为一个复选框,因此用户可以选择是否包含它。
用户控制器:
[HttpPost]
public ActionResult Create(CreateUserViewModel model)
{
if (ModelState.IsValid)
{
User newUser = new User();
newUser.UserName = model.UserName;
newUser.Password = model.Password;
newUser.Email = model.Email;
newUser.Products = model.Products; //Always NULL
当我在应用程序的这一部分设置断点时,会填充用户名密码和电子邮件的属性,但产品和语言为空。我使用 Fiddler2 来观察传入控制器的内容,这是输出:
UserName=asdasdsdasd&Password=asd&Email=asD%40asd.com&type.IsSelected=Admin
&prod.ID=1&prod.IsSelected=true&prod.IsSelected=false
&prod.ID=2&prod.IsSelected=false
&prod.ID=3&prod.IsSelected=false
&prod.ID=4&prod.IsSelected=false
&lang.ID=1&lang.IsSelected=true&lang.IsSelected=false
&lang.ID=2&lang.IsSelected=true&lang.IsSelected=false
&lang.ID=3&lang.IsSelected=false
&lang.ID=4&lang.IsSelected=false
&lang.ID=5&lang.IsSelected=false
&lang.ID=6&lang.IsSelected=false
&lang.ID=7&lang.IsSelected=false
&lang.ID=8&lang.IsSelected=false
&lang.ID=9&lang.IsSelected=false
&lang.ID=10&lang.IsSelected=false
&lang.ID=11&lang.IsSelected=false
&lang.ID=12&lang.IsSelected=false
&lang.ID=13&lang.IsSelected=false
&lang.ID=14&lang.IsSelected=false
&lang.ID=15&lang.IsSelected=false
&lang.ID=16&lang.IsSelected=false
&lang.ID=17&lang.IsSelected=false
&lang.ID=18&lang.IsSelected=false
&lang.ID=19&lang.IsSelected=false
创建视图的代码块是:
<div class="editor-label">
@Html.Label("User Products:")
</div>
<div class="editor-field">
@foreach (var prod in Model.Products)
{
@Html.Label(prod.Name)
@Html.HiddenFor(x => prod.ID)
@Html.CheckBoxFor(x => prod.IsSelected, new { name = prod.Name })
}
</div>
<div class="editor-label">
@Html.Label("User Languages:")
</div>
<div class="editor-field">
@foreach (var lang in Model.Languages)
{
@Html.Label(lang.Name)
@Html.HiddenFor(x => lang.ID)
@Html.CheckBoxFor(x => lang.IsSelected, new { name = lang.Name })
}
</div>
<p>
<input type="submit" value="Create" />
</p>
我已经为此工作了很长一段时间,我开始认为我想做的事情是不可能的。我希望它返回CreateUserViewModel
完全填充的用户选择的所有值,但我只是不知道如何实现这一点。
有任何想法吗?