How to bind nested objects on HttpPost in ASP.NET MVC

前端 未结 1 1852
清酒与你
清酒与你 2021-01-07 10:19

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

相关标签:
1条回答
  • 2021-01-07 11:08

    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");
    }
    
    0 讨论(0)
提交回复
热议问题