【发布时间】:2013-12-24 07:31:50
【问题描述】:
我有一个 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 为空。我知道 ASP.NET MVC 能够绑定嵌套类型,但在这段代码中不能。我也可以通过以下方式获取电子邮件和电话值:
var email = Request.Form["UserInput.Email"];
var phone = Request.Form["UserInput.Phone"];
也许我做错了什么!这是一个简单的模型绑定,您可以在网络上随处找到。
【问题讨论】:
-
你能显示呈现的电子邮件或电话的 html 吗?
-
将 UserInput 转换为 CheckoutViewModel 类中的属性
-
@SatpalSingh:是的,你是对的。我忘了让它成为财产。谢谢
标签: asp.net-mvc asp.net-mvc-4 model-binding