I'm trying to display a button because I need to add new information but there is a list within the file and it is not working
I am using ASP.NET Core MVC and C#.
It is something like this:
HTML File:
@model IEnumerable<MyApplication.Models.Clients>
<table class="table" id="Tabla4">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
My controller method:
public ActionResult Create()
{
List<Clients> clients = dbContext.GetClients().ToList();
return View(clients);
}
I want to add this in my HTML file:
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CODE" class="control-label"></label>
<input asp-for="CODE" class="form-control" />
<span asp-validation-for="CODE" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NAME" class="control-label"></label>
<input asp-for="NAME" class="form-control" />
<span asp-validation-for="NAME" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
I tried to add this:
@Model MyApplication.Models.Clients
I get this error:
The model directive may only occur once per document
How can I solve it?
EDIT:
My Client Model is this:
public class Clients{
public int CODE {get; set;}
public string NAME {get; set;}
}
If i put the button inside the HTML File it throws and error:
@model IEnumerable<MyApplication.Models.Clients>
<table class="table" id="Tabla4">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CODE" class="control-label"></label>
<input asp-for="CODE" class="form-control" />
<span asp-validation-for="CODE" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NAME" class="control-label"></label>
<input asp-for="NAME" class="form-control" />
<span asp-validation-for="NAME" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
It says:
IEnumerable<MyApplication.Models.Clients> does not contain a definition CODE and no accesible extension method CODE
IEnumerable<MyApplication.Models.Clients> does not contain a definition NAME and no accesible extension method NAME
EDIT #2
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label for="CODE" class="control-label"></label>
<input name="CODE" class="form-control" />
</div>
<div class="form-group">
<label for="NAME" class="control-label"></label>
<input name="NAME" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
EDIT #3:
Now it is throwing this error: Object reference not set to an instance of an object, in the foreach part:
@model IEnumerable<MyApplication.Models.Clients>
<div class="row">
<div class="col-md-4">
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label for="CODE" class="control-label">Code:</label>
<input name="CODE" class="form-control" />
</div>
<div class="form-group">
<label for="NAME" class="control-label">Name:</label>
<input name="NAME" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Insert" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<table class="table" id="Tabla4">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.CODE)
</th>
<th>
@Html.DisplayNameFor(model => model.NAME)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CODE)
</td>
<td>
@Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
</td>
</tr>
}
</tbody>
</table>
Image: [![image][1]][1]
This is my Controller Create HTTP METHOD:
public ActionResult Create([Bind] Clients clients)
{
try
{
if (ModelState.IsValid)
{
dbContext.InsertData(clients);
return RedirectToAction("Index");
}
return View(clients);
}
catch
{
return View();
}
}