2015-12-01 61 views
3

我的模型是:如何使用c#在ASP.NET MVC中的運行時動態添加行?

public partial class transaction 
{ 
    public int transaction_id { get; set; } 
    public Nullable<int> transaction_req_no { get; set; } 
    public Nullable<System.DateTime> transaction_date { get; set; } 
    public Nullable<int> transaction_reqst_by { get; set; } 
    public Nullable<int> transaction_priority { get; set; } 
    public Nullable<int> transaction_item { get; set; } 
    public Nullable<int> transaction_site { get; set; } 
    public Nullable<int> transaction_dept { get; set; } 
    public Nullable<int> transaction_reqst_status { get; set; } 

    public virtual department department { get; set; } 
    public virtual item item { get; set; } 
    public virtual person person { get; set; } 
    public virtual priority priority { get; set; } 
    public virtual request request { get; set; } 
    public virtual site site { get; set; } 
    public virtual status status { get; set; } 
} 

的視圖:

<div class="form-horizontal"> 
     <h4>transaction</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_req_no, "transaction_req_no", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("transaction_req_no", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.transaction_req_no, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_date, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.transaction_date, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.transaction_date, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_reqst_by, "transaction_reqst_by", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("transaction_reqst_by", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.transaction_reqst_by, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_priority, "transaction_priority", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("transaction_priority", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.transaction_priority, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


      <div class="form-group"> 
       @Html.LabelFor(model => model.transaction_item, "transaction_item", htmlAttributes: new { @class = "control-label col-md-2" }) 

       <div class="col-md-4"> 
        @Html.DropDownList("transaction_item", null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.transaction_item, "", new { @class = "text-danger" }) 
       </div> 
      </div> 




     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_site, "transaction_site", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("transaction_site", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.transaction_site, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_dept, "transaction_dept", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("transaction_dept", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.transaction_dept, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.transaction_reqst_status, "transaction_reqst_status", htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownList("transaction_reqst_status", null, htmlAttributes: new { @class = "form-control" }) 
       @Html.ValidationMessageFor(model => model.transaction_reqst_status, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 

} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

和控制器被從腳手架生成。沒有額外的代碼添加在那裏。我想添加多個交易項目。 public Nullable<int> transaction_item { get; set; }腳手架視圖爲此生成一個下拉列表。我需要一個可以從前端添加多個項目的習慣。

I would like to add this element dynamically.

我讀thisthisthisfirst一個看起來很有前途,但我仍然無法獲得如何添加數據到數據庫,即使我可以通過使用jQuery在前端添加元素?

+1

這就是我的觀點生成的腳手架。 –

+0

您顯示的圖像是一種形式。您所顯示的代碼沒有表單或任何表單控件。顯示正確的代碼。 –

+1

我很抱歉。只是更新了正確的代碼。 –

回答

1

使用JQuery創建它,然後使用jquery prop InserAfter()並添加新行。

例如:

var r = $("HTML CODE"); 
$(r).insertAfter($("#BlaBlaBla")); 
+0

喜歡這個?? http://www.codeproject.com/Articles/781851/Add-Delete-Rows-Dynamically-using-jQuery-in-ASP-NE –

+0

這也適用,查看關於insertAfter()函數的jquery頁面 –

1

至於前臺是相關的,你可以看到answer to this。您可以直接使用FormCollection而不是模型。

codelen1

codelen2

codelen3

,那麼你可以創建一個後臺的方法來接受的FormCollection作爲參數:然後,你可以爲指定的值,你行。在這種方法中,你可以循環的行數如下:

[HttpPost] 
publict ActionResult DynamicRows(FormCollection f) 
{ 
    int i = Convert.ToInt32(f["level"]); // you will set the number of rows added from JQuery 
    for (int a = 0; a <= i; a++) 
    { 
     ABC lvl = new ABC(); 
     lvl.S1 = a.ToString(); 
     lvl.N1 = a; 
     lvl.N2 = Convert.ToInt32(f["codelen" + a.ToString()]); // here you are getting back the values as codelen1..codelen2..codelen3 

     // your other logic comes here... anything you want to do with the data 
     WebDb.ABC.Add(lvl); 

    } 
    WebDb.SaveChanges(); 
} 
相關問題