asp.net-mvc 将表值从视图传递到控制器 MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27896202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:22:08  来源:igfitidea点击:

Pass Table Value from View to Controller MVC

asp.net-mvcrazorengine

提问by Vinod

Can I pass table td values to controller?

我可以将表 td 值传递给控制器​​吗?

View strongly typed:

查看强类型:

@using (Html.BeginForm("PostClick", "Vendor", FormMethod.Post)) {
<table class="tblData">
  <tr>
    <th>
      @Html.DisplayNameFor(model => model.First().SubmittedDate)
    </th>
    <th>
      @Html.DisplayNameFor(model => model.First().StartDate)
    </th>
  </tr>

  <tr>
    <td>
      @Html.DisplayFor(modelItem => item.SubmittedDate)
    </td>
    <td>
      @Html.DisplayFor(modelItem => item.StartDate)
    </td>
   </tr>
 </table>
 <input type="submit" value="submit" />
    }

Contoller code:

控制器代码:

public void PostClick(FormCollection collection)
{
   /*Some Code */
} 

How to pass table value from view to controller?

如何将表值从视图传递到控制器?

Have used JasonData & Ajax call and able to send the table data to controller.

使用了 JasonData & Ajax 调用并且能够将表数据发送到控制器。

Want to know any other method can be done because FormCollectiondata not able to find table values

想知道任何其他方法可以完成,因为FormCollection数据无法找到表值

回答by

Your need to generate controls that post back (input, textareaor select) and generate those controls in a forloop (or use a custom EditorTemplatefor type Vendor)

您需要生成回发(input,textareaselect)的控件并在for循环中生成这些控件(或使用自定义EditorTemplatetype Vendor

View

看法

@model List<Vendor>
@using (Html.BeginForm())
{
  <table class="tblData">
    <thead>
      ....
    </thead>
    <tbody>
      for(int i = 0; i < Model.Count; i++)
      {
        <tr>
          <td>@Html.TextBoxFor(m => m[i].SubmittedDate)</td>
          <td>@Html.TextBoxFor(m => m[i].StartDate)</td>
        </tr>
      }
    </tbody>
  </table>
  <input type="submit" value="submit" />
}

Post method

邮寄方式

public void PostClick(List<Vendor> model)
{
  /*Some Code */
}