-1

i try to search this problem, but could not find a solution for it, please help me:

my viewModel:

namespace webShop.ViewModels.Home
{
    public class RegisterUserViewModel
    {
        public User User { get; set; }

        public UsersDetaile UsersDetaile { get; set; }

        public IEnumerable<state> state { get; set; }
    }
}

my model:

public partial class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Roles { get; set; }
    public byte Status { get; set; }

    public virtual UsersDetaile UsersDetaile { get; set; }
}

public partial class UsersDetaile
{
    public int userID { get; set; }
    public Nullable<System.DateTime> BirthDate { get; set; }
    public string Mobile { get; set; }
    public string Tell { get; set; }
    public bool Gender { get; set; }
    public byte city { get; set; }
    public byte state { get; set; }
    public string postCode { get; set; }
    public string address { get; set; }

    public virtual City City1 { get; set; }
    public virtual State State{ get; set; }
    public virtual User User { get; set; }
}

and my controller:

[HttpGet]
public ActionResult Register()
{ 
    OstanRepository stateRep = new OstanRepository();
    var model = new RegisterUserViewModel();
    model.state = stateRep.Select().ToList();
    return View(model);
}

[HttpPost]
public ActionResult Register(RegisterUserViewModel user)
{
   //My Code
}

and my register view

@model webShop.ViewModels.Home.RegisterUserViewModel

@using (Ajax.BeginForm("Register", "Home", new AjaxOptions { HttpMethod = "Post", Url = "/Home/Register" }))
{

  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true)
  <h1 class="block-header">Register</h1>
      <div class="group">
          <label class="label">Name <span class="required">*</span></label>
          <div class="controls">
              @Html.TextBoxFor(p => p.User.Name, new { @class = "text" })
              @Html.ValidationMessageFor(p => p.User.Name)
          </div>
      </div>

    //....

  <div class="group">
        <label class="label">Address </label>
        <div class="controls">
            @Html.TextBoxFor(p => p.UsersDetaile.address, new { @class = "text" })
            @Html.ValidationMessageFor(p => p.UsersDetaile.address)
        </div>
  </div>

  <div class="form">
    <div class="group">
        <div class="controls">
             <button class="button">Submit</button>
        </div>
    </div>
  </div>
}

after press submit user and usersDetaile is null, what can i do?


Of course, if i use this acction, it's worke!!!

public ActionResult Register(User user)
{
    //...
}

i cant find a solution, PLEASE HELP ME

mmz
  • 35
  • 1
  • 6

2 Answers2

1

You are doing wrong what i see, you have to pass ViewModel to you View, currently you are just passing the User property of ViewModel to your view which means your View is strongly typed to User type not RegisterUserViewModel, thats the reason when you change action parameter to User it works:

[HttpGet]
public ActionResult Register()
{ 
    OstanRepository stateRep = new OstanRepository();
    var model = new RegisterUserViewModel();
    model.state = stateRep.Select().ToList();
    return View(model); // pass ViewModel to view not only User Model
}

and you view should be strongly type with RegisterUserViewModel:

On view set model to RegisterUserViewModel

@model webShop.ViewModels.Home.RegisterUserViewModel
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

The binding fails because you POST method parameter is named user and your model (RegisterUserViewModel) contains property with the same name (public User User { get; set; })

Change you method signature to

[HttpPost]
public ActionResult Register(RegisterUserViewModel model)

and the properties will be correctly bound. Alternatively, you can change the property name but this would mean updating your view code

`public User Person { get; set; }`