ASP MVC4 空模型传递给控制器中的操作
ASP MVC4 null model passed to action in controller
我想知道为什么我得到从视图到控制器的空模型。
这是我在视图中的代码 (UpdatePersonal.cshtml):
@model Project.Models.UserInfo
@using (Html.BeginForm()){
@Html.LabelFor(m => m.userinfo.firstname);
@Html.TextBoxFor(m => m.userinfo.firstname, new { @Value = ViewBag.Firstname });
@Html.LabelFor(m => m.userinfo.lastname);
@Html.TextBoxFor(m => m.userinfo.lastname, new { @Value = ViewBag.Lastname });
@Html.LabelFor(m => m.userinfo.email);
@Html.TextBoxFor(m => m.userinfo.email, new { @Value = ViewBag.Email });
@Html.LabelFor(m => m.userinfo.phone);
@Html.TextBoxFor(m => m.userinfo.phone, new { @Value = ViewBag.Phone });
@Html.HiddenFor(m => m.username, new { @Value = ViewBag.Username });
<input type="submit" value="Submit" />}
这是接受它的操作方法:
[HttpPost]
[AllowAnonymous]
public ActionResult UpdatePersonal(UserInfo userInfo){
//some code here
//my breakpoint}
我看到正在传递的模型具有空值,因为我使用了断点
我的模特:
public class UserInfo
{
[BsonId]
public string username { get; set; }
public Info userinfo { get; set; }
public Address address { get; set; }
public class Info
{
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Address
{
public string street { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string postalcode { get; set; }
public string country { get; set; }
}
}
我刚刚解决了我的问题,我使用并传递了子类
@model Buch_Ankauf.Models.UserInfo.Info
@using (Html.BeginForm()){
@Html.LabelFor(m => m.firstname);
@Html.TextBoxFor(m => m.firstname, new { @Value = ViewBag.Firstname });
@Html.LabelFor(m => m.lastname);
@Html.TextBoxFor(m => m.lastname, new { @Value = ViewBag.Lastname });
@Html.LabelFor(m => m.email);
@Html.TextBoxFor(m => m.email, new { @Value = ViewBag.Email });
@Html.LabelFor(m => m.phone);
@Html.TextBoxFor(m => m.phone, new { @Value = ViewBag.Phone });
@Html.Hidden("username", new { @Value = ViewBag.Username });
<input type="submit" value="Submit" />}
在我的控制器中:
[HttpPost]
[AllowAnonymous]
public ActionResult UpdatePersonal(UserInfo.Info userInfo)
{
你解决了这个问题,但你的第一个代码很好,唯一的问题是你的操作方法参数的名称与你的模型的名称相同 属性。
更改您的操作方法签名,例如:
public ActionResult UpdatePersonal(UserInfo info)
应该可以!
我想知道为什么我得到从视图到控制器的空模型。
这是我在视图中的代码 (UpdatePersonal.cshtml):
@model Project.Models.UserInfo
@using (Html.BeginForm()){
@Html.LabelFor(m => m.userinfo.firstname);
@Html.TextBoxFor(m => m.userinfo.firstname, new { @Value = ViewBag.Firstname });
@Html.LabelFor(m => m.userinfo.lastname);
@Html.TextBoxFor(m => m.userinfo.lastname, new { @Value = ViewBag.Lastname });
@Html.LabelFor(m => m.userinfo.email);
@Html.TextBoxFor(m => m.userinfo.email, new { @Value = ViewBag.Email });
@Html.LabelFor(m => m.userinfo.phone);
@Html.TextBoxFor(m => m.userinfo.phone, new { @Value = ViewBag.Phone });
@Html.HiddenFor(m => m.username, new { @Value = ViewBag.Username });
<input type="submit" value="Submit" />}
这是接受它的操作方法:
[HttpPost]
[AllowAnonymous]
public ActionResult UpdatePersonal(UserInfo userInfo){
//some code here
//my breakpoint}
我看到正在传递的模型具有空值,因为我使用了断点
我的模特:
public class UserInfo
{
[BsonId]
public string username { get; set; }
public Info userinfo { get; set; }
public Address address { get; set; }
public class Info
{
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Address
{
public string street { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string postalcode { get; set; }
public string country { get; set; }
}
}
我刚刚解决了我的问题,我使用并传递了子类
@model Buch_Ankauf.Models.UserInfo.Info
@using (Html.BeginForm()){
@Html.LabelFor(m => m.firstname);
@Html.TextBoxFor(m => m.firstname, new { @Value = ViewBag.Firstname });
@Html.LabelFor(m => m.lastname);
@Html.TextBoxFor(m => m.lastname, new { @Value = ViewBag.Lastname });
@Html.LabelFor(m => m.email);
@Html.TextBoxFor(m => m.email, new { @Value = ViewBag.Email });
@Html.LabelFor(m => m.phone);
@Html.TextBoxFor(m => m.phone, new { @Value = ViewBag.Phone });
@Html.Hidden("username", new { @Value = ViewBag.Username });
<input type="submit" value="Submit" />}
在我的控制器中:
[HttpPost]
[AllowAnonymous]
public ActionResult UpdatePersonal(UserInfo.Info userInfo)
{
你解决了这个问题,但你的第一个代码很好,唯一的问题是你的操作方法参数的名称与你的模型的名称相同 属性。
更改您的操作方法签名,例如:
public ActionResult UpdatePersonal(UserInfo info)
应该可以!