如何使用MVC4 .net中的Model将表单数据和html表数据从视图发布到控制器

时间:2015-03-10 12:08:24

标签: .net asp.net-mvc asp.net-mvc-4 asp.net-ajax html-table

我尝试了很多东西 像

"var htmlTableValue = "<HTML>
<table>
 <tr><TD> 12 <TD></tr>
   </table>
   </HTML>";

      var request = $.ajax({
            url: '',//action method url which defined in controller
            type: 'POST',
            cache: false,
            data: JSON.stringify(htmlTableValue),
            dataType: 'json',
            contentType: 'application/json; charset=utf-8'
        });`enter code here`"

但它甚至没有工作,甚至这个在stackoverflow上的答案被标记为正确答案。 Actualy我想做的是在我的视图中有一个html表在前端添加行,在结尾处保存按钮从视图中保存整个数据,就像我在视图中也有一些输入文本框。这是我的代码< / p>

       @model MyPumpERP.Models.SalesOrder
         @{
         Layout = "~/Views/Shared/_Layout_Business.cshtml";
         ViewBag.Title = "New sales Contract";
          }
   <!DOCTYPE html>



   <html lang="en">
     <head>
   <script src="~/Assets/js/jquery.js"></script>
    <link href="~/Assets/css/PrintArea.css" rel="stylesheet" />
     <script src="~/Assets/js/jquery.PrintArea.js"></script>
     </head>
       <body >
       <div class="col-md-6 col-sm-6 col-xs-12">
                            <div class="form-group">
                                    <label for="fax" class="col-sm-6 control-label" style="text-align:left">@Resources.Resource.Fax</label>
                                    <div class="col-sm-6">
                                     <input type="text" class="form-control" id="7" placeholder="+111-000-999">
                                    </div>
                             </div>
                             <div class="form-group">
                                   <label for="cellphone" class="col-sm-6 control-label" style="text-align:left">@Resources.Resource.CellPhone</label>
                                    <div class="col-sm-6">
                                      <input type="text" class="form-control" id="8" placeholder="+111-000-999">
                                   </div>
                              </div>
                           <div class="form-group">
                                   <label for="Currency" class="col-sm-6 control-label" style="text-align:left">@Resources.Resource.Currency</label>
                                    <div class="col-sm-6">
                                      <input type="text" class="form-control" id="9" placeholder="0000">
                                   </div>
                              </div>

                </div>
                  </div>
                <div class="row">
                    <div id="table" class="table-editable">
                       <span class="table-add glyphicon glyphicon-plus"></span>


                    <table class="table">
                                        <tr>
                                           <th>@Resources.Resource.Project</th>
                                           <th>@Resources.Resource.Brand</th>
                                            <th>@Resources.Resource.Model</th>
                                           <th>@Resources.Resource.Name</th>
                                            <th>@Resources.Resource.Quantity</th>
                                           <th>@Resources.Resource.Unit</th>
                                            <th>@Resources.Resource.PriceCost</th>
                                           <th>@Resources.Resource.SubTotal</th>
                                           <th></th>
                                           <th></th>
                                           </tr>
                           @foreach (var item in Model.productdetail)
                 {

                                   <tr>
                           <td contenteditable="true">@item.ProductName</td>
                             <td contenteditable="true">
                                 <select class="currentCulture form-control" >

                                      <option value="0" title="@Url.Content("~/Content/Images/flag_En.png")">1</option>
                                      <option value="1" title="@Url.Content("~/Content/Images/flag_Ro.png")" selected="selected">2</option>
                                      <option value="2" title="@Url.Content("~/Content/Images/flag_De.png")">3</option>

                                 </select>

                             </td>
                             <td contenteditable="true">@item.Name</td>
                             <td contenteditable="true">@item.Id</td>
                             <td contenteditable="true">@item.Model</td>
                             <td contenteditable="true">@item.Unit</td>
                             <td contenteditable="true">@item.Price</td>
                             <td contenteditable="true">@item.Type</td>
                             <td>
                                   <span class="table-remove fa fa-times-circle"></span>
                             </td>
                             <td>
                             <span class="table-up fa fa-sort-asc"></span>
                            <span class="table-down fa fa-sort-desc"></span>
                              </td>
                              </tr>

  <!-- This is our clonable table line -->
                           <tr class="hide">
                                 <td contenteditable="true">Untitled</td>
                                <td contenteditable="true">  
                                    <select class=" form-control" >

                                        <option value="0" title="@Url.Content("~/Content/Images/flag_En.png")">1</option>
                                       <option value="1" title="@Url.Content("~/Content/Images/flag_Ro.png")" selected="selected">2</option>
                                       <option value="2" title="@Url.Content("~/Content/Images/flag_De.png")">3</option>

                        </select></td>
                               <td contenteditable="true">Untitled</td>
                                <td contenteditable="true">undefined</td>
                               <td contenteditable="true">Untitled</td>
                                <td contenteditable="true">undefined</td>
                               <td contenteditable="true">Untitled</td>
                                <td contenteditable="true">undefined</td>
                                <td>
                                <span class="table-remove fa fa-times-circle"></span>
                                </td>
                                <td>
                                <span class="table-up fa fa-sort-asc"></span>
                                <span class="table-down fa fa-sort-desc"></span>
                                </td>
                           </tr>
                             }    
                     </table> 

                   </div>


                </div>

Productdetail是我在salesorder模型中的产品的列表类型。 如果有任何人可以帮助我,我将非常感谢。

这是我的模型

public class SalesOrder
{
    public long Id { get; set; }
    public string SalesOrderId { get; set; }
    public Nullable<long> SalesOrderDeatilId { get; set; }
    public Nullable<long> SalesOrdertrackingNumber { get; set; }
    public Nullable<System.DateTime> DueDate { get; set; }
    public Nullable<System.DateTime> OrderDate { get; set; }
    public string OrderStatus { get; set; }
    public Nullable<long> OfficeId { get; set; }
    public Nullable<long> CustomerId { get; set; }
    public Nullable<long> EmployeeId { get; set; }
    //detail

    public string ShippingAddress { get; set; }
    public string BillingAddress { get; set; }
    public string CurrencyType { get; set; }
    public Nullable<long> SubTotal { get; set; }
    public Nullable<long> Total { get; set; }
    public Nullable<long> Quantity { get; set; }
    public Nullable<long> ProductId { get; set; }
    public Nullable<long> UnitPrice { get; set; }

    public List<Product> productdetail { get; set; }

}

1 个答案:

答案 0 :(得分:0)

看起来因为您只是将表转换为JSON,所以您不一定要回复您所说的模型。此外,它并不是您发布的表单。我目前无法测试这个,但我建议尝试两件事。首先,您尝试将ActionResult上的参数更改为

早期假设(历史)

你能在控制器中提供方法吗?

是否使用以下属性进行修饰?

[HttpPost]

或者问题不是你不能发帖,而是你不能张贴表格?如果是这种情况,我建议您查看您的模型是如何设置的 - 您是否可以使用您的模型编辑主帖。

[HttpPost] 
public ActionResult NewSalesContract(FormCollection formCollection) 
{
 ...
} 

[HttpPost] 
public ActionResult NewSalesContract(List<string> model) 
{
 ...
} 

看看他们是否有任何价值观。但是我不推荐使用它们,即使它们工作,而是制作一个强类型模型,你会期望得到什么,然后你可以使用类似原始签名的东西。

也许检查发布的数据的值(JSON),然后使模型匹配?