插入多个HTML表格

时间:2016-10-11 11:00:26

标签: html asp.net asp.net-mvc model-view-controller

非常新的asp MVC,我有一个包含多个HTML表的视图,我的问题是如何将这些表中的数据传递给我的控制器,传递的数据将进入相同的SQL表

1 个答案:

答案 0 :(得分:1)

假设你有3个表,每个表都有一些输入控件,那么你的MVC Action应该接受一个类的实例,这个类应该为你的HTML中的每个输入控件都有一个公共属性。

例如: 假设您有以下HTML

using System;

public class Program
{
    public static void Main()
    {
        string s = "aasfkkfasfas";
        int count = 0;
        foreach(char c in s)
        {
            if(c == 'k') 
                count++;
        }

        Console.WriteLine(count);
    }
}

然后你的Action参数应该类似于:

<table>
    <tr>
        <td><input type="text" name="ProductName" value=" " /></td>
    </tr>
    <tr>
        <td><input type="text" name="ProductPrice" value=" " /></td>
    </tr>
    <tr>
        <td><input type="text" name="ProductDescription" value=" " /></td>
    </tr>
</table>

你的行动应该是:

public class FormData
{
  public string ProductName{get;set;}
  public string ProductPrice{get;set;}
  public string ProductDescription{get;set;}
}

添加的表格和控件越多,添加到 [HttpPost] public ActionResult Create(FormData form) { if (ModelState.IsValid) { try { } catch (Exception ex) { } return RedirectToAction("Index"); } return View(form); } 类的属性就越多,如果要添加分组,则可以在FormData类中添加子类,使用它为HTML中的属性名称添加前缀。