【问题标题】:Inserts to multiple HTML Tables插入多个 HTML 表格
【发布时间】:2016-10-11 11:00:25
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    例如: 假设您有以下 HTML

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

    那么你的 Action 参数应该类似于:

    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类的属性越多,如果你想添加分组,那么你可以在FormData类中添加子类并在属性名称前加上前缀在 HTML 中使用它。

    【讨论】:

    • 唯一的问题是,有很多控件,因此我们会有很多属性,如果有的话,我们正在寻找更清洁的解决方案
    猜你喜欢
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多