【问题标题】:Orchard Widget for front end DB Access用于前端数据库访问的 Orchard 小部件
【发布时间】:2017-06-04 11:14:24
【问题描述】:

我是 Orchard 开发、C# 和 MVC 的新手,如果这真的很简单,请原谅我......

我在我的职业中使用过 Orchard CMS,因此我了解小部件、图层区域、内容部分等......但是,我的任务是改进其中一个非常常见的流程,我决定最好的方法是是为此目的创建一个模块。

使用小部件的基本流程如下:

  1. 客户导航到一个页面并显示三个部分,提交和查看
  2. “提交”部分是一个小部件,它是一个表单(很可能是自定义表单),它发布到控制器,并将该数据提交到数据库,我想我已经弄清楚了......但要确保我我将使用以下内容来执行此操作:

        [HttpPost]
    public ActionResult Index(string fName, string lName) {
        // var post values
        string fName = Request.Form["fName"];
        string lName = Request.Form["lName"];
    
        System.Data.SqlClient.SqlConnection sqlConnection1 =
            new System.Data.SqlClient.SqlConnection(@"[CONNECTION STRING]");
    
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = "INSERT INTO Persons (FirstName, LastName) VALUES ('" + fName + "','" + lName +"')";
        cmd.Connection = sqlConnection1;
    
        sqlConnection1.Open();
        cmd.ExecuteNonQuery();
        sqlConnection1.Close();
    
        return View();
    }
    

但是,我不确定这是否是正确的方法,并且我确信有更好的方法可以做到这一点,教程建议我使用 contentParts 和 contentPart 记录将数据提交到数据库但是,这仅在从管理端提交数据时有用,这必须能够由最终用户从前端提交

  1. 第二部分是一个表格,它会从数据库中获取记录列表并将它们显示给用户,但是,对此我完全不知道如何去做,谁能给我一个教程,示例源代码,甚至代码 sn-ps 在哪里实现?

值得注意的是,我已经浏览了 Orchard 网站上创建模块和小部件的文档,但是,它们都是通过后端更新数据库...

  • 奥达提亚

【问题讨论】:

    标签: c# orchardcms orchardcms-1.10


    【解决方案1】:

    虽然您的解决方案似乎可行,但我会以完全不同的方式实现它。

    首先,您将连接到 orchard 数据库,因此您可以使用 Orchard 的机制来访问数据库。假设您通过迁移创建了表,您可以使用IRepository 接口访问它。

    请注意,您的端口模型必须在 /Models 目录中!

    /Models/Port.cs:

    public class Port {
        public virtual int Id { get; set; }
        public virtual string MBN { get; set; }
        public virtual string Partner { get; set; }
    }
    

    Migrations.cs:

    public int Create() {
        SchemaBuilder.CreateTable("Port",
            table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
                .Column<string>("MDN", column => column.NotNull().WithDefault(""))
                .Column<string>("Partner", column => column.NotNull().WithDefault(""))
            );
        return 1;
    }
    

    /Controllers/PortingController.cs:

    [Themed]
    public class PortingController : Controller
    
        private readonly IRepository<Port> _repository;
    
        public PortingController(IRepository<Port> repository) {
            _repository = repository;
        }
    
        [HttpGet]
        public ActionResult Index() {
            // query the table
            var ports = _repository.Table.ToList();
            return View(ports);
        }
    
        [HttpPost]
        public ActionResult AddRequest(port item) {
            if (ModelState.IsValid) {
                _repository.Create(item);
            }
    
            return RedirectToAction("Index");
        }
    
    }
    

    然后在您的 Views/Porting/Index.cshtml 中:

    @using Porting.Models
    @model IEnumerable<Port>
    
    @* display the models.. *@
    
    @* display a form for creating a new one *@
    
    @using (Html.BeginFormAntiForgeryPost(Url.Action("CreateRequest", "Port", new { area = "YourAreaName" }))) {
        // the form
    }
    

    参考见this post

    【讨论】:

      【解决方案2】:

      对于任何偶然发现这篇文章寻求帮助的人,我按以下方式完成了列出的两件事:

      控制器:

       [Themed]
          public class PortingController : Controller
          {
              // GET Porting/Index
              public ActionResult Index()
              {
                  List<port> ports = new List<port>();
                  string constr = "Data Source=127.0.0.1;Port=3307;Database=orchard;User Id=root;Password=usbw ";
                  using (MySqlConnection con = new MySqlConnection(constr))
                  {
                      string query = "SELECT * FROM icc_porting_icc_activesoftswitch_ports";
                      using (MySqlCommand cmd = new MySqlCommand(query)) 
                      {
                          cmd.Connection = con;
                          con.Open();
                          using (MySqlDataReader sdr = cmd.ExecuteReader()) {
                              while (sdr.Read()) 
                              {
                                  ports.Add(new port {
                                      Id = Convert.ToInt32(sdr["Id"]),
                                      MBN = sdr["MBN"].ToString(),
                                      Partner = sdr["Partner"].ToString()
                                  });
                              }
                          }
                      }
                      con.Close();
                  }
                  return View(ports);
              }
      
              // POST AddRequest
              [HttpPost]
              public ActionResult AddRequest(FormCollection forms)
              {
                  // init vars
                  string mbn = forms["PortingRequestForm.mbn.Value"];
                  string partner = forms["PortingRequestForm.Partner.Value"];
      
                  // db con string
                  string connString = "Data Source=127.0.0.1;Port=3307;Database=orchard;User Id=root;Password=usbw ";
                  MySqlConnection conn = new MySqlConnection(connString);
                  conn.Open();
                  MySqlCommand comm = conn.CreateCommand();
                  comm.CommandText = "INSERT INTO icc_porting_icc_activesoftswitch_ports(mbn, partner) VALUES(?mbn, ?partner)";
                  comm.Parameters.AddWithValue("?mbn", mbn);
                  comm.Parameters.AddWithValue("?partner", partner);
                  comm.ExecuteNonQuery();
                  conn.Close();
      
                  //string endContent = mbn + " " + partner;
                  return RedirectToAction("Index");
              }
          }
      }
      

      然后模型只是简单地声明一些基本属性

      为了添加到数据库中,我创建了一个自定义表单并使用 jQuery 来更改表单操作,因此它没有使用 orchard 的自定义表单控制器,而是发布到以下内容:~/{moduleName}/{Controller}/{Action}

      然后只需使用标准剃须刀标记从索引控制器示例中获取变量:

      @using Porting.Models
      @model IEnumerable<port>
          <table class="demo">
              <thead>
                  <tr>
                      <th>ID</th>
                      <th>MBN</th>
                      <th>Status</th>
                  </tr>
              </thead>
              <tbody>
                  @foreach (port Port in Model) {
                      <tr>
                          <td>@Port.Id</td>
                          <td>@Port.MBN</td>
                          <td>@Port.Partner</td>
                      </tr>
                  }
              <tbody>
      </table>
      

      这不是最好的,我知道可能有一百万种方法可以改进它,所以如果有人不介意帮助,请给我发私信,甚至在这里发表评论?

      谢谢

      • 奥达提亚

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 2014-09-09
        • 2019-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多