简体   繁体   English

提交后如何获取价值 - ASP.NET CORE MVC

[英]How to get value after Submit - ASP.NET CORE MVC

I'm having trouble retrieving the value for DropDownList.我无法检索 DropDownList 的值。 This is my Create.cshtml:这是我的 Create.cshtml:

@model BikeService.Models.Bike

@{
    ViewData["Title"] = "Add new Bike";
}

<form method="post" asp-action="Create">
                <div class="form-group row">
                    <div class="col-4">
                        <label asp-for="Customer" class=""></label>
                    </div>
                    <div class="col-8 dropdown">
                        @{
                            var options = new SelectList(@ViewBag.ListOfCustomers, "Id", "Name");

                            @Html.DropDownList("customers", options);
                        }
                    </div>
                </div>
    </div>
</form>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

The data for the DropDownList comes in fine. DropDownList 的数据很好。

<div class="col-8 dropdown">
  <select id="customers" name="customers">
    <option value="3384186f-a3c4-4afe-a5be-12544a91acb7">Alex</option>
    <option value="93b68f45-9c24-45e1-afc0-4549610b7c0d">John</option>
    <option value="456502ca-f08f-49e6-ad90-d32be262ed58">Petter<option>
  </select>
</div>

This is the controller这是 controller

[HttpPost]
        public async Task<IActionResult> Create(Bike bike)
        {
            await _bikeServices.AddBike(bike);
            return View();
        }

Although in option in dropdownlist is assigned correctly, to controller at CustomerID value field is empty, selected value is not sent.尽管下拉列表中的选项已正确分配,但 controller 的 CustomerID 值字段为空,不会发送所选值。

从表单发送到控制器的对象

The mvc framework maps the name of the input element to the property of the model when the form is posted.当提交表单时,mvc 框架将输入元素的名称映射到 model 的属性。

So the dropdownlist name should be CustomerID not customers .所以下拉列表名称应该是CustomerID而不是customers Try:尝试:

@Html.DropDownList("CustomerID", options)

Now the select element will be rendered like:现在 select 元素将呈现为:

<select id="CustomerID" name="CustomerID">
<option value="3384186f-a3c4-4afe-a5be-12544a91acb7">Alex</option>
<option value="93b68f45-9c24-45e1-afc0-4549610b7c0d">John</option>
<option value="456502ca-f08f-49e6-ad90-d32be262ed58">Petter<option>
</select>

and the selected value will be posted back in the Bike model in the CustomerID property.并且选定的值将在CustomerID属性的Bike model 中回传。

I have seen this where you accidentally send NULL instead of the GUID value.我看到你不小心发送了 NULL 而不是 GUID 值。 It might be worth putting this in your Configure in Startup.cs (assuming common setup), so that you can examine the JSON POST.可能值得将它放在 Startup.cs 中的配置中(假设常见设置),以便您可以检查 JSON POST。 Alternatively, POST it to PostMan from your front end.或者,从前端将其发布到 PostMan。

app.Use(async(context, next) =>
{
    using (var reader = new System.IO.StreamReader(context.Request.Body))
    {
        var body = reader.ReadToEnd();

        // Output to console or logger, or just add a break-point here
        Console.WriteLine(body);
    }
}

If that doesn't help, share your "Bike" class DTO so we can replicate.如果这没有帮助,请分享您的“自行车”class DTO,以便我们进行复制。

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

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