Reputation: 1166
I wonder why i get null model passing from the view to the controller.
Here is my code in the view (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" />}
Here is the action method that accepts it:
[HttpPost]
[AllowAnonymous]
public ActionResult UpdatePersonal(UserInfo userInfo){
//some code here
//my breakpoint}
i see the model being passed has null value as i used breakpoint
my model:
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; }
}
}
Upvotes: 0
Views: 688
Reputation: 1794
You work around the problem but your first code was good, the only problem was the name of your action method param was the same that the name of your model property.
Change your action method signature for example by :
public ActionResult UpdatePersonal(UserInfo info)
and it should be work !
Upvotes: 2
Reputation: 1166
I just solved my problem, instead i used and pass the subclass
@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" />}
in my controller:
[HttpPost]
[AllowAnonymous]
public ActionResult UpdatePersonal(UserInfo.Info userInfo)
{
Upvotes: 0