mrminer mrminer - 6 days ago 6
ASP.NET (C#) Question

C# Table add new rows with textbox

I got a question.
I'm making a table but I need a add page where the user can fill in a textbox, (or something else) Then press add. If this happens the table needs to be updated with the information the user added.

Could some one help me? Because I do not know how to start.

The code from the table page:

@model IEnumerable<DNDB.Models.Domeinnaam>

@Styles.Render("~/Content/StyleSheet.css")
@{
ViewBag.Title = "Index";
}
<h2>Domeinnaam Overzicht</h2>

@Html.ActionLink("add", "CreateDomeinnaam.cshtml")


<table id="tabledomeinnamen">
<tr>
<th>
@Html.DisplayNameFor(model => model.IsActief)
</th>
<th>
@Html.DisplayNameFor(model => model.Naam)
</th>
<th>
@Html.DisplayNameFor(model => model.TLD)
</th>
<th>
@Html.DisplayNameFor(model => model.DatumRegistratie)
</th>
<th>
@Html.DisplayNameFor(model => model.Omschrijving)
</th>
<th>
@Html.DisplayNameFor(model => model.DatumOpzeg)
</th>
<th>
@Html.DisplayNameFor(model => model.EigenaarID)
</th>
<th>
@Html.DisplayNameFor(model => model.Opmerking)
</th>
<th>
@Html.DisplayNameFor(model => model.BeheerAccountID)
</th>
<th>
@Html.DisplayNameFor(model => model.KlantID)
</th>
<th>
@Html.DisplayNameFor(model => model.RegistrarID)
</th>
<th>
@Html.DisplayNameFor(model => model.BetaaldVan)
</th>
<th>
@Html.DisplayNameFor(model => model.BetaaldTot)
</th>
<th>
@Html.DisplayNameFor(model => model.AfspraakPrijs)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.IsActief)
</td>
<td>
@Html.DisplayFor(modelItem => item.Naam)
</td>
<td>
@Html.DisplayFor(modelItem => item.TLD)
</td>
<td>
@Html.DisplayFor(modelItem => item.DatumRegistratie)
</td>
<td>
@Html.DisplayFor(modelItem => item.Omschrijving)
</td>
<td>
@Html.DisplayFor(modelItem => item.DatumOpzeg)
</td>
<td>
@Html.DisplayFor(modelItem => item.EigenaarID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Opmerking)
</td>
<td>
@Html.DisplayFor(modelItem => item.BeheerAccountID)
</td>
<td>
@Html.DisplayFor(modelItem => item.KlantID)
</td>
<td>
@Html.DisplayFor(modelItem => item.RegistrarID)
</td>
<td>
@Html.DisplayFor(modelItem => item.BetaaldVan)
</td>
<td>
@Html.DisplayFor(modelItem => item.BetaaldTot)
</td>
<td>
@Html.DisplayFor(modelItem => item.AfspraakPrijs)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID })
@Html.ActionLink("Details", "Details", new { id=item.ID })
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}

</table>

Answer

The code from the table page:

@Html.ActionLink("add", "CreateDomainName", "Domain") //Second parameter is the action name, Third parameter is the controller name. If your 'list'(table) page action and 'add' action are in the same controller then this field is optional.

CreateDomainName action in your controller.

public ActionResult CreateDomainName()
{
   return View();
}

[HttpPost]
public ActionResult CreateDomainName(Domeinnaam model)
{
   if(ModelState.IsValid)
   {
      //Your code to save
   }

   return RedirectToAction("Index");
}

To generate the view of CreateDomainName action, right click on action and click on 'add view'.

View of CreateDomainName action.

@using(Html.BeginForm())
{
   <!-- Your html code -->
   <input type="submit">Add</input>
}
Comments