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  !
    //...
}
7
  • 1
    Your editing data so ALWAYS use a view model and you never need an awful [Bind] attribute when using a view model Commented Dec 5, 2017 at 1:35
  • @StephenMuecke Thanks, bro! Please help with some working solution. Commented Dec 5, 2017 at 1: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? Commented Dec 5, 2017 at 1:39
  • @StephenMuecke userRequest.UserRequest on HttpPost should have inputed values but they are empty. Commented Dec 5, 2017 at 1: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! Commented Dec 5, 2017 at 1:43

1 Answer 1

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.