2017-06-04 29 views
1

我是新來的果業發展,C#和MVC,所以請原諒我,如果這其實很簡單就是...果園小部件的前端DB訪問

我用果園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. 第二部分是表格它會從數據庫中獲取記錄列表並將它們顯示給用戶,但是,爲此我不知道如何去做這件事,任何人都可以指向我的教程,示例源代碼或甚至代碼片段這是實現的嗎?

值得注意的是,我已經通過文件走了果園的網站創建模塊和部件,但是,它們都通過後臺更新DB ...

  • Odatia

回答

1

雖然你的解決方案似乎工作,我會實現它完全不同。

首先,您正在連接到orchard db,因此您可以使用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"); 
    } 

} 

然後在您的瀏覽/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

0

穿過這片後絆人尋求幫助,我做了兩件事上市以下列方式:

控制器:

[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來改變形式的行動,從而代替使用果園的自定義表單控制器,它發佈到以下內容:〜/ {模塊名}/{控制器}/{行動}

然後用標準的剃鬚刀標記,將其從指數控制器例如GET變量:

@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> 

它不是最好的,我知道如果有人不介意幫助,請給我發私人訊息,甚至只是在這裏發表評論?

感謝

  • Odatia