0

私はC#プログラミングに少し慣れていないので、ある場所で立ち往生しました。君の力が必要。実際には、javascriptを使用して、ユーザーボタンのクリックごとにテーブル(4列)の行(ユーザーが入力できるようにテキストボックスを含む)を生成しています。行数が固定されておらず、テキストボックスの正確な名前がわからないため、これらの行をsqlserverテーブルにどのように挿入するかが問題になります。

ユーザーボタンをクリックするたびにテキストボックスの名前を生成するために、単純にループを使用する必要がありますか?すべてのコントロールの名前を取得したら、ループを使用して1つの挿入ステートメントでこれらすべてを挿入できますか?

暖かいリグラード、アミー

4

2 に答える 2

0

MVC を使用していると仮定しています。次のようなモデルを作成することから始めることができます。

public class YourModel
{
    public IEnumerable<Users> users { get; set; }
}

ビューを作成し、以下のスクリプトで行を動的に追加するよりも:

<script type="text/javascript">

var Rows = 1;
// We already got the 0 element as html so start from 1

function AddUser() {
    $("#UserTable").append('<tr>' +
        '<td><input type="text" name="users[' + Rows + '].Name" style="width:100px;" /></td>' +
        '<td><input type="text" name="users[' + Rows + '].Surname" style="width:100px;" /></td>' +
        '<td><input type="text" name="users[' + Rows + '].Age" style="width:50px;" /></td>' +
        '<td><input type="text" name="users[' + Rows + '].Date" style="width:70px;" /></td>' +
        '</tr>');
    // Add datepicker (this is an optional jQueryUI stuff)
    $('input[name="users[' + Rows + '].Date"]').datepicker({ dateFormat: 'yy.mm.dd' });
    // Go to next row
    Rows = Rows + 1;
}

$(document).ready(function(){
    // Create an empty row on load
    AddUser();
    // Than on each click add another row
    $('input[type=button]').click(function(){ AddUser(); });
});

</script>

<div>
    <table id="UserTable">
        <tr>
            <td><input type="text" name="user[0].Name" style="width:100px;" value="Berker" /></td>
            <td><input type="text" name="user[0].Surname" style="width:100px;" value="Yüceer" /></td>
            <td><input type="text" name="user[0].Age" style="width:50px;" value="24" /></td>
            <td><input type="text" name="user[0].Date" style="width:70px;" value="2012.12.11" /></td>
        </tr>
    </table>
    <input type="button" id="add" value="Add" />
</div>

スクリプトのフィドル: http://jsfiddle.net/BerkerYuceer/YFecD/

コントローラーから、以下に示すように値を取得できます。

// Thanks to LinqToSql you can define ur Sql-DB this way
YourDBDataContext db = new YourDBDataContext();

//
// POST: /YourForm/Edit
[HttpPost]
public ActionResult Edit(YourModel model)
{
    try
    {
        // if users not empty..
        if (model.users != null)
        {
            // Each User in Users
            foreach (var user in model.users)
            {   // Save it to your Sql-DB
                db.Users.InsertOnSubmit(user);
                db.SubmitChanges();
            }
        }
        // Return
        return RedirectToAction("Index");
    }
    catch (Exception ex)
    {
        return RedirectToAction("Error", new { ErrorMessage = "Message[" + ex.Message + "] - Source[" + ex.Source + "] - StackTrace[" + ex.StackTrace + "] - Data[" + ex.Data + "]" });
    }
}

//
// GET: /YourForm/Error
public ActionResult Error(String ErrorMessage)
{
    ViewData["Error"] = ErrorMessage;
    return View();
}

このように簡単!

于 2012-12-12T08:36:32.070 に答える
0

If you're using just a regular html table add 'runat="server"' Tag with JS as well as html form controls then just change TableRow to HtmlTableRow and TextBox to HtmlInputText. All of those controls are in the System.Web.UI.HtmlControls namespace.

Assuming you're using the Table server control then it's just:

foreach (TableRow row in table.Rows)
    {
       var col2 = (TextBox)row.Cells[1].Controls[0];
       //Do DB inserting stuff here With col2
       string stringToInsert=  col2 .Text;
    }
于 2012-12-12T07:37:44.537 に答える