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")
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();
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<CurrencyMapping> CurrencyMapping { get; set; }
}
}