1

I have started to implement this solution bind attribute include and exclude property with complex type nested objects

but it does not work at all.

The same question is here Binding nested model with MVC3 on HttpPost

but no concrete answer.

Model

[Bind(Include = "FirstName,MiddleName,LastName,Position,TruckTypeID,Direction,Organization,Objective,TimeStart,TimeEnd")]
public partial class UserRequestRegisterModel
{
    [DisplayName("Имя")]
    [Required]
    public string FirstName { get; set; }

    [DisplayName("Фамилия")]
    [Required]
    public string MiddleName { get; set; }

    [DisplayName("Отчество")]
    [Required]
    public string LastName { get; set; }

    [DisplayName("Должность")]
    [Required]
    public string Position { get; set; }

    [DisplayName("Тип транспорта")]
    [Required]
    public System.Guid TruckTypeID { get; set; }

    [DisplayName("Направление")]
    [Required]
    public string Direction { get; set; }

    [DisplayName("Организация")]
    [Required]
    public string Organization { get; set; }

    [DisplayName("Цель")]
    [Required]
    public string Objective { get; set; }

    [DisplayName("Время убытия")]
    [Required]
    // [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy  HH:mm}", ApplyFormatInEditMode = true)]
    public System.DateTime TimeStart { get; set; }

    [DisplayName("Время прибытия")]
    [Required]
    // [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
    public Nullable<System.DateTime> TimeEnd { get; set; }        
}


[Bind(Include = "UserRequest")]
public partial class RequestUserModel
{
    public List<UserRequestViewItem> UserRequestViewItems { get; set; }
    public UserRequestRegisterModel UserRequest { get; set; }

    public RequestUserModel()
    {
        UserRequestViewItems = new List<UserRequestViewItem>();
        UserRequest = new UserRequestRegisterModel();
    }
}

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "FirstName,MiddleName,LastName,Position,TruckTypeID,Direction,Organization,Objective,TimeStart,TimeEnd")] RequestUserModel userRequest)
//public ActionResult Index(RequestUserModel userRequest)
{
    // !  userRequest.UserRequest is empty  !
    //...
}
adiga
  • 34,372
  • 9
  • 61
  • 83
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Your editing data so ALWAYS use a view model and you never need an awful `[Bind]` attribute when using a view model –  Dec 05 '17 at 01:35
  • @StephenMuecke Thanks, bro! Please help with some working solution. – NoWar Dec 05 '17 at 01:37
  • _but it does not work at all._ - What does not work? What are you wanting to achieve? Just create view models that represent what you want in the view and get rid of your `[Bind]` attributes - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Dec 05 '17 at 01:39
  • @StephenMuecke `userRequest.UserRequest` on HttpPost should have inputed values but they are empty. – NoWar Dec 05 '17 at 01:41
  • 1
    Because the `[Bind]` attribute in your POST method states - _include only the properties named `"FirstName", "MiddleName"`, etc_, and your `RequestUserModel` does not contain any properties with those names (only the `UserRequest` property contains those names). Again, use a view model! –  Dec 05 '17 at 01:43
  • @StephenMuecke Ok, but what do I have to do then? – NoWar Dec 05 '17 at 01:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160470/discussion-between-stephen-muecke-and-academy-of-programmer). –  Dec 05 '17 at 01:45

1 Answers1

1

After 30 minutes of freestyle experimenting I finally get it working.

So, guys, solution is following

Model

 [Bind(Include = "FirstName,MiddleName,LastName,Position,TruckTypeID,Direction,Organization,Objective,TimeStart,TimeEnd")]
    public partial class UserRequestRegisterModel
    {
        [DisplayName("Имя")]
        [Required]
        public string FirstName { get; set; }

        [DisplayName("Фамилия")]
        [Required]
        public string MiddleName { get; set; }

        [DisplayName("Отчество")]
        [Required]
        public string LastName { get; set; }

        [DisplayName("Должность")]
        [Required]
        public string Position { get; set; }

        [DisplayName("Тип транспорта")]
        [Required]
        public System.Guid TruckTypeID { get; set; }

        [DisplayName("Направление")]
        [Required]
        public string Direction { get; set; }

        [DisplayName("Организация")]
        [Required]
        public string Organization { get; set; }

        [DisplayName("Цель")]
        [Required]
        public string Objective { get; set; }

        [DisplayName("Время убытия")]
        [Required]
        // [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy  HH:mm}", ApplyFormatInEditMode = true)]
        public System.DateTime TimeStart { get; set; }

        [DisplayName("Время прибытия")]
        [Required]
        // [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> TimeEnd { get; set; }        
    }


public partial class RequestUserModel
    {
        public List<UserRequestViewItem> UserRequestViewItems { get; set; }
        public UserRequestRegisterModel UserRequest { get; set; }

        public RequestUserModel()
        {
            UserRequestViewItems = new List<UserRequestViewItem>();
            UserRequest = new UserRequestRegisterModel();
        }
    }

HTML

@model TransportRequests.Models.RequestUserModel
...

@using (Html.BeginForm("CreateUserRequest", null, FormMethod.Post, null))
{
   @Html.EditorFor(model => model.UserRequest.FirstName, new { htmlAttributes = new { @class = "form-control", @style = "width:200px" } })
}

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateUserRequest([Bind(Include = "UserRequest")] RequestUserModel model)
{
     // and here all properties has inputed values... YEAH!!!  :)

     var firstName = model.UserRequest.FirstName;

     return RedirectToAction("Index");
}
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Interesting, still I'm with @StephenMuecke, the usage of the `BindAttribute` is awful and this code is a perfect example why you should get rid of it – thmshd Dec 05 '17 at 07:54