简体   繁体   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#)

I have written this button but it is not working fine, it is not invoking the create HTTP method in the controller file:我已经写了这个按钮,但它不能正常工作,它没有调用 controller 文件中的 create HTTP 方法:

Tale a look:故事看一下:

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

After clicking on "Insert" button it does not do anything and my Create method is not even called in the Controller file (null parameters)单击“插入”按钮后,它什么也不做,我的 Create 方法甚至没有在 Controller 文件中调用(空参数)

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

Been searching and trying to solve it but it seems I'm not doing it correctly, I'm super lost in here.一直在搜索并尝试解决它,但似乎我做得不对,我在这里迷失了方向。 What should i correct?我应该纠正什么?

Can help me?可以帮我?

Your view receive a collection of MyApplication.Models.Clients and when you click on the submit button, you send a collection of Clients to controller.您的视图会收到MyApplication.Models.Clients的集合,当您单击提交按钮时,您会将一组Clients发送到 controller。

So if you want to send an object of type Clients from view to controller, you should create another action and view that receive one model of MyApplication.Models.Clients like bellow.因此,如果您想将Clients类型的 object 从视图发送到 controller,您应该创建另一个操作和视图来接收MyApplication.Models.Clients的 model。

Create view:创建视图:

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