Exemple #1
0
 public string _getCtrlString(InputItem inp, string idx)
 {
     StringBuilder sb = new StringBuilder();
     switch (inp.InputType)
     {
         case "text":
             sb.AppendFormat("<input name=\"{0}[{3}].{1}\" id=\"{1}_{3}\" type=\"text\" class=\"form-control {2}row_{1}\" />"
                 , this.ChildCollectionName, inp.PascalName, this.ChildCollectionName.ToLower(), idx);
             break;
         case "hidden":
             sb.AppendFormat("<input name=\"{0}[{3}].{1}\" id=\"{1}_{3}\" type=\"hidden\" class=\"{2}row_{1}\" />"
                 , this.ChildCollectionName, inp.PascalName, this.ChildCollectionName.ToLower(), idx);
             break;
         case "select":
             sb.AppendFormat("<select name=\"{0}[{3}].{1}\" id=\"{1}_{3}\" class=\"form-control {2}row_{1}\">"
                 , this.ChildCollectionName, inp.PascalName, this.ChildCollectionName.ToLower(), idx);
             sb.Append("<option value=\"\">全部</option>");
             sb.Append("</select>");
             break;
         case "radio":
             sb.AppendFormat("<input name=\"{0}[{3}].{1}\" id=\"{1}_{3}\" type=\"radio\" class=\"form-control {2}row_{1}\" />"
                 , this.ChildCollectionName, inp.PascalName, this.ChildCollectionName.ToLower(), idx);
             break;
         case "checkbox":
             sb.AppendFormat("<input name=\"{0}[{3}].{1}\" id=\"{1}_{3}\" type=\"checkbox\" class=\"form-control {2}row_{1}\" />"
                 , this.ChildCollectionName, inp.PascalName, this.ChildCollectionName.ToLower(), idx);
             break;
         case "textarea":
             sb.AppendFormat("<textarea name=\"{0}[{3}].{1}\" id=\"{1}_{3}\" class=\"form-control {2}row_{1}\" ></textarea>"
                 , this.ChildCollectionName, inp.PascalName, this.ChildCollectionName.ToLower(), idx);
             break;
         default:
             break;
     }
     return sb.ToString();
 }
Exemple #2
0
        private void btnGen_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtParentObjectName.Text) || string.IsNullOrWhiteSpace(txtParentObjectName.Text))
            {
                return;
            }
            if (string.IsNullOrEmpty(txtNamespace.Text) || string.IsNullOrWhiteSpace(txtNamespace.Text))
            {
                return;
            }
            if (string.IsNullOrEmpty(txtWebProjNameSpace.Text) || string.IsNullOrWhiteSpace(txtWebProjNameSpace.Text))
            {
                return;
            }

            bool hasForeignKey = false;
            string foreignKey = cbForeignKey.Text.Trim();
            if (string.IsNullOrEmpty(foreignKey))
            {
                return;
            }
            foreach (var item in SelectedChildTable.Columns)
            {
                if (item.PascalName == foreignKey)
                {
                    hasForeignKey = true;
                    break;
                }
            }
            if (!hasForeignKey)
            {
                return;
            }

            bool hasChildTable = false;
            string tbChildName = cbTablesForChild.Text.Trim();
            if (string.IsNullOrEmpty(tbChildName))
            {
                return;
            }
            foreach (var item in Database.Tables)
            {
                if (item.Name == tbChildName)
                {
                    SelectedChildTable = item;
                    hasChildTable = true;
                    break;
                }
            }
            if (!hasChildTable)
            {
                return;
            }

            bool hasParentTable = false;
            string tbParentName = cbTablesForParent.Text.Trim();
            if (string.IsNullOrEmpty(tbParentName))
            {
                return;
            }
            foreach (var item in Database.Tables)
            {
                if (item.Name == tbParentName)
                {
                    SelectedParentTable = item;
                    hasParentTable = true;
                    break;
                }
            }
            if (!hasParentTable)
            {
                return;
            }

            string childCollectionName = txtChildName.Text.Trim();
            if (string.IsNullOrEmpty(childCollectionName))
            {
                return;
            }

            if(tbParentName == tbChildName)
            {
                MessageBox.Show("父对象和子对象不能是同一个表", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            txtChildName.Enabled = false;
            txtNamespace.Enabled = false;
            txtWebProjNameSpace.Enabled = false;
            cbTablesForChild.Enabled = false;
            cbTablesForParent.Enabled = false;
            gvFields.Enabled = false;
            btnGen.Enabled = false;
            btnGen.Text = "正在生成...";
            btnView.Hide();

            //设置SimpleChildGenModel
            GenModel.ParentObjectName = txtParentObjectName.Text.Trim();
            GenModel.ForeignKey = foreignKey;
            GenModel.NameSpacePR = txtNamespace.Text.Trim();
            GenModel.WebProjNameSpace = txtWebProjNameSpace.Text.Trim();
            GenModel.ChildCollectionName = childCollectionName;
            GenModel.ChildModel = this.SelectedChildTable;
            GenModel.ParentModel = this.SelectedParentTable;
            GenModel.Items.Clear();
            foreach (DataGridViewRow item in gvFields.Rows)
            {
                InputItem inp = new InputItem();
                string rawName = item.Cells[0].Value.ToString();
                inp.Title = item.Cells[3].Value.ToString();
                inp.PascalName = StringHelper.SetPascalCase(rawName);
                inp.CamelName = StringHelper.SetCamelCase(rawName);
                inp.InputType = item.Cells[4].Value.ToString();
                GenModel.Items.Add(inp);
            }

            AspnetMVCSetting setting = new AspnetMVCSetting();
            setting.Namespace = GenModel.NameSpacePR;
            setting.WebProjNameSpace = GenModel.WebProjNameSpace;
            SettingStore.Instance.Save<AspnetMVCSetting>(setting, this.Database.Name + "_aspnetmvc.xml");

            BackgroundWorker _bgWorker = new BackgroundWorker();
            _bgWorker.WorkerSupportsCancellation = false;
            _bgWorker.WorkerReportsProgress = false;
            _bgWorker.DoWork += _bgWorker_DoWork;
            _bgWorker.RunWorkerCompleted += _bgWorker_RunWorkerCompleted;
            _bgWorker.RunWorkerAsync();
        }