model指令只能在每个文档中出现一次(将按钮添加到问题列表中)ASP.NET MVC Core C#

model指令只能在每个文档中出现一次(将按钮添加到问题列表中)ASP.NET MVC Core C#,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我试图显示一个按钮,因为我需要添加新信息,但文件中有一个列表,它不起作用 我正在使用ASP.NET核心MVC和C# 是这样的: <div class="form-group"> <label for="CODE" class="control-label">Code:</label> <input name="CODE" class="form-co

我试图显示一个按钮,因为我需要添加新信息,但文件中有一个列表,它不起作用

我正在使用ASP.NET核心MVC和C#

是这样的:

<div class="form-group">
    <label for="CODE" class="control-label">Code:</label>
    <input name="CODE" class="form-control" />
</div>
HTML文件:

@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>
我得到这个错误:

model指令在每个文档中只能出现一次

我怎样才能解决它


编辑:

我的客户模型是:

public class Clients{
 public int CODE {get; set;}
public string NAME {get; set;}

}
如果我将按钮放在HTML文件中,它会抛出错误:

   @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>

当您使用的模型/值不具有这些属性时,您不能使用asp for=“…”。您的模型被定义为
IEnumerable
,因为视图的值是
列表
对象。使用
时,它会在
IEnumerable
类型中查找属性
code
。但是,此接口没有这样的属性

在您的情况下,不需要
asp for=“”
。相反,根据您定义的
Create()
操作,您可以使用带有所需
name=“…”
属性的“普通”标记。代码可以如下所示:

<div class="form-group">
    <label for="CODE" class="control-label">Code:</label>
    <input name="CODE" class="form-control" />
</div>

代码:

这样,HTTP请求将在POST正文或GET URL参数列表中包含字段名
code
,具体取决于发送表单的方式。ASP.NET将自动将这些值填入
Create()
操作的参数中。

如果您使用的是ASP.NET Core MVC,请使用
ASP.NET Core MVC
标记来说明这一点!为什么要在视图中放置两行
@Model
?你的文档/视图中的模型是什么类型?@marc_抱歉,我的坏:(@Progman你说的“类型”是什么意思,我的客户模型包含两个东西:代码和NAME@programming_amazing我指的是您调用文档/视图时使用的模型类型。您只能使用一行
@model…
(很可能靠近/位于顶部)表示如下内容:“此文档/视图使用类型为
YourChooseType
的模型”。使用两行
@Model
是没有意义的,因为只有一个值,如果您添加两行
@Model
,您会得到预期的错误消息。非常感谢您的回复,朋友。看一看,我已经编辑了我的问题。它应该会触发创建[HTTP POST]方法,对吗?我相信您需要
method=“post”
属性发送一个
POST
请求。否则它将是一个
GET
请求。我不完全理解:它不是通过使用来执行POST请求吗?我是否应该编辑它,并放入如下内容:?@programming\u惊人的是。您可以在浏览器中检查生成的源代码,应该会看到类似
。这样发送POST请求。
   @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>
<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>
@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>
public ActionResult Create([Bind] Clients clients)
{
    try
    {
        if (ModelState.IsValid)
        {
            dbContext.InsertData(clients);
            return RedirectToAction("Index");
        }
        return View(clients);
    }
    catch
    {
        return View();
    }
}
<div class="form-group">
    <label for="CODE" class="control-label">Code:</label>
    <input name="CODE" class="form-control" />
</div>