控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var myViewModel = new CreateFavoriteListViewModel();
var favoriteTypeDropdownList = new List<SelectListItem>();
favoriteTypeDropdownList.Add(new SelectListItem { Text = "Text1", Value = "1" });
favoriteTypeDropdownList.Add(new SelectListItem { Text = "Text2", Value = "2" });
myViewModel.FavoriteTypeDropdownList = favoriteTypeDropdownList;
return View(myViewModel);
}
[HttpPost]
public ActionResult Post(CreateFavoriteListInputModel createFavoriteListInputModel)
{
return new EmptyResult();
}
}
楷模:
public class CreateFavoriteListViewModel
{
public CreateFavoriteListInputModel CreateFavoriteListInputModel { get; set; }
public List<SelectListItem> FavoriteTypeDropdownList { get; set; }
}
public class CreateFavoriteListInputModel
{
[Required]
public string ListName { get; set; }
public int SelectedFavoriteType { get; set; }
}
看法:
@model MvcApplication3.Controllers.CreateFavoriteListViewModel
<h2>title</h2>
@using (Html.BeginForm("Post", "Home", FormMethod.Post))
{
@Html.LabelFor(x => x.CreateFavoriteListInputModel.ListName)
@Html.TextBoxFor(x => x.CreateFavoriteListInputModel.ListName)
@Html.LabelFor(x => x.CreateFavoriteListInputModel.SelectedFavoriteType)
@Html.DropDownListFor(x => x.CreateFavoriteListInputModel.SelectedFavoriteType, Model.FavoriteTypeDropdownList)
<input type="submit" value="Save" id="btnCreateList" name="btnCreateList" />
}
如您所见,我使用带有特殊 lambda 表达式的输入模型。( x=>x.CreateFavoriteListInputModel.ListName
)。奇怪的问题是,这确实适用于我的家用计算机,但不适用于我的公司一台(createFavoriteListInputModel = Null
)。似乎有不同版本的 ASP.NET MVC4 或类似的东西。
也许你们中的某些人知道,ASP.NET MVC 何时支持这种模型绑定。
我的代码对你有用吗?
如果我将视图模型和 Lamba 表达式更改为 iex=>x.ListName
一切都在两台计算机上运行。