我有一个以下的ViewModel:
public class CheckoutViewModel
{
public string ProductNumber { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public Input UserInput;
public class Input
{
public string Email { get; set; }
public string Phone { get; set; }
}
}
还有这样一种操作:
[HttpPost]
public ActionResult Index(CheckoutViewModel model)
{
// ...
return View();
}
我的模型绑定如下:
@model GameUp.WebUI.ViewModels.CheckoutViewModel
@using (Html.BeginForm("Index", "Checkout", FormMethod.Post))
{
@Html.AntiForgeryToken()
<!-- some HTML -->
@Html.LabelFor(m => m.UserInput.Email)
@Html.TextBoxFor(m => m.UserInput.Email)
@Html.LabelFor(model => model.UserInput.Phone)
@Html.TextBoxFor(model => model.UserInput.Phone)
<button>Submit</button>
}
当我提交表单时,UserInput为null。我知道ASP.NET MVC能够绑定嵌套类型,但在此代码中不能。同时,我可以通过以下方式获取电子邮件和电话的值:
var email = Request.Form["UserInput.Email"];
var phone = Request.Form["UserInput.Phone"];
也许我做错了什么!这是一个简单的模型绑定,你可以在网上找到它的相关内容。