如何将HTML表传递给控制器​​然后传递给mysql表 [英] How to pass HTML table to controller then to mysql table

查看:96
本文介绍了如何将HTML表传递给控制器​​然后传递给mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是我要做的事情的要点:

使用VS2015的MvcMovie ASP.NET网站教程,我利用了它自动创建的CRUD视图。我还创建了自己的实体Currency类,类似于MySql表。我使用DataAdapter和Datatable从Mysql加载数据,将DataTable转换为我的Currency Class的IEnumerable并将其作为模型传递给我的视图,在这种情况下,我将传递给我的索引。我最终想要做的是使我的视图中的表像一个可编辑的表,以便用户不必只是为了添加,删除或更新每个记录而转到不同的页面。我通过穿上每个< td>来做到这一点。我的表

Ok so here's the gist of what I want to do:
Using VS2015's MvcMovie ASP.NET website tutorial, I leveraged the CRUD views it automatically created. I have also created my own entity Currency class that is similar to my MySql table. I loaded the data from Mysql using DataAdapter and Datatable, converted the DataTable to an IEnumerable of my Currency Class and passed it as the model to be used for my view, which for this case I am passing to my Index. What I ultimately want to do is make the table in my view like an editable table so that users don't have to go to different pages just to add, delete or update each records. I did this by putting on each <td> of my table

@Html.TextBox(item.CcyCode, item.CcyCode)

并在列表的末尾添加了一些

and at the end of the list added some

@Html.TextBox("CcyCode")

以满足新条目。



我的问题是,如何从我的索引传递显示的HTML表到我的控制器然后相应地更新我的MySql表。



如果有更简单和更好的方法,请建议。



到目前为止这些是我的代码:



我尝试过的事情:



到目前为止这些是我的代码:



控制器

to cater for new entries.

My issue is, how to pass the displayed HTML table from my Index to my Controller then update my MySql table accordingly.

If there is a simpler and better approach, please advice.

So far these are my codes:

What I have tried:

So far these are my codes:

Controller

private CurrencyMappingDBContext db = new CurrencyMappingDBContext();

// GET: /CurrencyMapping/
public ActionResult Index()
{
    DataTable dt = new DataTable();
    var dt2 = new MySystem.myDB_dbDataSet1.tbl_currencymappingDataTable();


    MySqlConnectionStringBuilder bldr = new MySqlConnectionStringBuilder();
    bldr.Server = "localhost";
    bldr.Database = "my_db";
    bldr.UserID = "x";
    bldr.Password = "x";
    MySqlConnection con = new MySqlConnection(bldr.ConnectionString);
    con.Open();
    MySqlDataAdapter da = new MySqlDataAdapter();
    MySqlCommand comm = new MySqlCommand("select * from tbl_currencymapping;", con);
    da.SelectCommand = comm;
    da.Fill(dt2);
    con.Close();

    var result = from row in dt2.AsEnumerable()
                 select new CurrencyMapping() { CcyName = row.CCY_NAME, CcyCode = row.CCY_CODE };

    return View(result.AsEnumerable());
}







索引视图




Index View

@model IEnumerable<MvcMovie.Models.CurrencyMapping>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("Save","CurrencyMapping",FormMethod.Post))
{
    @Html.AntiForgeryToken()
    
    @Html.ValidationSummary(true)
    
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CcyCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CcyName)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @*@Html.DisplayFor(modelItem => item.CcyCode)*@
                    @Html.TextBox(item.CcyCode, item.CcyCode)
                </td>
                <td>
                    @*@Html.DisplayFor(modelItem => item.CcyName)*@
                    @Html.TextBox(item.CcyName, item.CcyName)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.CcyCode }) |
                    @Html.ActionLink("Details", "Details", new { id = item.CcyCode }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.CcyCode })
                </td>
            </tr>
        }
        <tr>
            <td>
                @Html.TextBox("CcyCode")
            </td>
            <td>
                @Html.TextBox("CcyName")
            </td>
        </tr>

    </table>
    <div>
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
    
}





型号



Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MySystem;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcMovie.Models
{
    public class CurrencyMapping
    {
        [Key]
        public string MarketCcyCode { get; set; }
        public string CurrencyName { get; set; }
    }

    public class CurrencyMappingDBContext : DbContext
    {
        public DbSet&lt;CurrencyMapping&gt; CurrencyMapping { get; set; }
    }

}

推荐答案

而不是拥有完整数据集的表单,有表格对于每一行,并在每个行动作上调用相应的更新或删除方法。通过这种方式,您将一次更新1行。
Instead of having form for complete data set, have form for each row and call the respective update or delete method on each row action. By this way you will be updating 1 single row at a time.


这篇关于如何将HTML表传递给控制器​​然后传递给mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆