繁体   English   中英

我的 ASP.NET Core 按钮根本不发送任何内容(ASP.NET Core MVC 和 C#)

[英]My ASP.NET Core button not sending anything at all (ASP.NET Core MVC and C#)

我已经写了这个按钮,但它不能正常工作,它没有调用 controller 文件中的 create HTTP 方法:

故事看一下:

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

单击“插入”按钮后,它什么也不做,我的 Create 方法甚至没有在 Controller 文件中调用(空参数)

public ActionResult Create([Bind] Clients clients)
{
    try
    {
        if (ModelState.IsValid)
        {
            dbContext.InsertData(clients);
            return RedirectToAction("Index");
        }
        return View(clients);
    }
    catch
    {
        return View();
    }
}

一直在搜索并尝试解决它,但似乎我做得不对,我在这里迷失了方向。 我应该纠正什么?

可以帮我?

您的视图会收到MyApplication.Models.Clients的集合,当您单击提交按钮时,您会将一组Clients发送到 controller。

因此,如果您想将Clients类型的 object 从视图发送到 controller,您应该创建另一个操作和视图来接收MyApplication.Models.Clients的 model。

创建视图:

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

Controller:

[HttpGet]
public ActionResult Create()
{
     return View();
}
[HttpPost]
public ActionResult Create(Clients clients)
{
    try
    {
        if (ModelState.IsValid)
        {
            dbContext.InsertData(clients);
            return RedirectToAction("Index");
        }
        return View(clients);
    }
    catch
    {
        return View();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM