Editor add data in controller to insert to database

Editor add data in controller to insert to database

dpanscikdpanscik Posts: 121Questions: 32Answers: 0

In my MVC controller, I need to add two fields "userName" and "dateModified" before a record insert.

Here is the stock controller

        public IHttpActionResult DataTransport()
        {
            var settings = Properties.Settings.Default;
            var request = HttpContext.Current.Request;


            using (var db = new Database(settings.DbType, settings.DbConnection))
            {

                var response = new Editor(db, "Flex_OnCall_3Model", "TableID")
                    .Model<Flex_OnCall_3Model>()
                    .Field(new Field("dayOC")
                            .Validator(Validation.DateFormat(Format.DATE_ISO_2822))
                            .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_2822))
                            .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_2822))
                    )
                    .Debug(true)
                    .Process(request)
                    .Data();


                return Json(response);
            }
        }

Ive attempted this, but this is not the correct syntax

        public IHttpActionResult DataTransport()
        {
            var settings = Properties.Settings.Default;
            var request = HttpContext.Current.Request;


            using (var db = new Database(settings.DbType, settings.DbConnection))
            {

                var response = new Editor(db, "Flex_OnCall_3Model", "TableID")
                    .Model<Flex_OnCall_3Model>()
                    .Field(new Field("dayOC")
                            .Validator(Validation.DateFormat(Format.DATE_ISO_2822))
                            .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_2822))
                            .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_2822))
                    )
                    .Field("userName").SetValue("TestUsername")
                    .Field("dateMofified").SetValue.(DateTime.Now)
                    .Debug(true)
                    .Process(request)
                    .Data();


                return Json(response);
            }
        }

Answers

  • dpanscikdpanscik Posts: 121Questions: 32Answers: 0

    Here is the solution. Grab the form key value pairs and add the necessary data.

                var request = System.Web.HttpContext.Current.Request.Form;
                var propInfo = request.GetType().GetProperty("IsReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                propInfo.SetValue(request, false, new object[] { });
                request.Add("data[0][accountIdentifier]", accountIdentifier);
                request.Add("data[0][userName]", "userName");
                request.Add("data[0][modifiedDate]", DateTime.Now.ToString());
    
  • dpanscikdpanscik Posts: 121Questions: 32Answers: 0

    update....

    you need to remove the action string, add in your additional data, then put the action string at the end of the array

                var request = System.Web.HttpContext.Current.Request.Form;
                var propInfo = request.GetType().GetProperty("IsReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                propInfo.SetValue(request, false, new object[] { });
                int count = request.Count-1;
                if (request.Get(count) == "create" || request.Get(count) == "edit")
                {
                    var lastItem = request.Get(count);
                    request.Remove(request.GetKey(count)) ;
                    request.Add("data[0][accountIdentifier]", accountIdentifier);
                    request.Add("data[0][userName]", User.Identity.Name);
                    request.Add("data[0][modifiedDate]", DateTime.Now.ToString());
                    request.Add("action", lastItem);
                }
    
  • dpanscikdpanscik Posts: 121Questions: 32Answers: 0

    update 2...

    edit needs its own treatment

                var request = System.Web.HttpContext.Current.Request.Form;
                var propInfo = request.GetType().GetProperty("IsReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                propInfo.SetValue(request, false, new object[] { });
                int count = request.Count-1;
                if (request.Get(count) == "create")
                {
                    var lastItem = request.Get(count);
                    request.Remove(request.GetKey(count)) ;
                    request.Add("data[0][accountIdentifier]", accountIdentifier);
                    request.Add("data[0][userName]", User.Identity.Name);
                    request.Add("data[0][modifiedDate]", DateTime.Now.ToString());
                    request.Add("action", lastItem);
                }
                if (request.Get(count) == "edit")
                {
                    string rowIdString = request.GetKey(1);
                    string cutRowIdString = rowIdString.Substring(0, rowIdString.IndexOf('[', rowIdString.IndexOf('[') + 1));
                    var lastItem = request.Get(count);
                    request.Remove(request.GetKey(count));
                    request.Add(cutRowIdString + "[accountIdentifier]", accountIdentifier);
                    request.Add(cutRowIdString + "[userName]", User.Identity.Name);
                    request.Add(cutRowIdString + "[modifiedDate]", DateTime.Now.ToString());
                    request.Add("action", lastItem);
                }
    
    
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Thanks for posting back with your solution!

    Allan

Sign In or Register to comment.