C# ASP MVC4空模型传递给控制器中的操作
我想知道为什么会有空模型从视图传递到控制器 以下是我在视图中的代码(UpdatePersonal.cshtml): 当我使用断点时,我看到传递的模型有空值 我的模型:C# ASP MVC4空模型传递给控制器中的操作,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我想知道为什么会有空模型从视图传递到控制器 以下是我在视图中的代码(UpdatePersonal.cshtml): 当我使用断点时,我看到传递的模型有空值 我的模型: public class UserInfo { [BsonId] public string username { get; set; } public Info userinfo { get; set; } public Address address { get; set; } pu
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" />}
您解决了这个问题,但是您的第一个代码很好,唯一的问题是您的操作方法param的名称与您的模型属性的名称相同 更改动作方法签名,例如:
public ActionResult UpdatePersonal(UserInfo info)
这应该是工作 您的操作是否用
[HttpPost]
装饰?是的,我在操作中有[HttpPost]您的模型是什么样子的?@beautifulcoder我编辑了它。删除每个文本框的“new{@Value=ViewBag.XXX}Forjbbi答案是正确的解决方案。不能使用与模型属性之一相同的名称命名POST方法的参数。如评论中所述,不要试图使用new{@value=ViewBag.someValue}
hi覆盖value属性,我尝试了你的建议,但也发生了同样的事情。仍然无效。
@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)