Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
@Html.TextBox(item.CcyCode, item.CcyCode)
and at the end of the list added some
XML
@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
C#
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
HTML
@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
C#
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; }
    }

}
Posted
Updated 11-Oct-16 20:09pm

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900