将选择/选项值从包含列表模型的视图传递到控制器

问题描述 投票:0回答:2

我有一个模型列表,可以在 foreach 中调用以在选择/选项中显示,但是当我尝试将特定值发送回我的控制器以使其在终端中打印时,我得到以下信息:

LeagueProject.Models.ViewModel+Participant

我知道我传回的值是有效的,因为它显示在我的选项中的“id 号:”之后。

//my cshtml:
@model List<ViewModel.Participant>;
<form asp-action="PostNewBasketballPlayer2" asp-controller="Dashboard" method="POST">
<div class="form-group">
    <select name="ParticipantId">
    @{
            if(Model != null)
            {
                foreach(ViewModel.Participant i in Model)
                {
                    <option value="@i.ParticipantId">
                        @i.ParticipantFirstName @i.ParticipantLastName id number: @i.ParticipantId
                    </option>
                }                    
            }
    }
    </select>
</div>
<button class="btn btn-outline-info mt-5" type="submit">Sign up</button>
//my controller:
[HttpPost]
[Route("/Dashboard/postNewBasketballPlayer")]
public IActionResult PostNewBasketballPlayer2(ViewModel.Participant newParticipant)
    {
        Console.WriteLine(newParticipant);
        return RedirectToAction("Dashboard","Dashboard");
    }

我尝试过的:

我更改了值以查看是否可以获得不同的结果,但我从终端得到了相同的响应

示例:

<option value="12">

我还尝试将传递给控制器的内容更改为 int 和 string

public IActionResult PostNewBasketballPlayer2(int newParticipant)

当我将其更改为 int 时,我在终端中为我选择的任何选项返回 0,即使没有一个 id 为零。

public IActionResult PostNewBasketballPlayer2(string newParticipant)

当我将其更改为字符串时,我什么也没得到。

我还尝试制作一个完整独立的 Participant 模型,因为 ViewModel.Participant 是包含 Participant 的包装模型。

public IActionResult PostNewBasketballPlayer2(Participant newParticipant)

终端打印的内容:

LeagueProject.Models.Participant

但是,我想继续使用包装器模型以保持代码的一致性。这只是我进行的一个测试,看看是否能得到不同的结果。

c# asp.net asp.net-mvc asp.net-core asp.net-core-mvc
2个回答
0
投票

根据提供的信息,我在 bitbucket 上创建了一个存储库。 ParticipantId 可以正确传递:


0
投票

代码问题:

路由模板一致性:

确保

[Route]
属性中指定的路由模板与您期望访问操作的 URL 路径匹配。在本例中,该操作需要对 /Dashboard/postNewBasketballPlayer 发出 POST 请求。

型号用途:

在代码的开头,你已经定义了

@model List<ViewModel.Participant>
,但是在实际代码中,
ViewModel.Participant
作为表单提交的参数,可能会导致数据绑定的问题。您应该使用包含客户注册所需所有字段的输入模型(例如ParticipantId)。

表格中的选择: 在表单中,您使用了

<select>
元素来选择参与者。但是,您需要确保每个值都对应于每个参与者的唯一 ParticipantId。

发送数据到控制器:PostNewBasketballPlayer2 控制器中,您需要一个 ViewModel.Participant 类型的对象。如果仅需要提交ParticipantId,则此假设可能不正确。应确保表格正确接收注册所需的数据。

可能的修复:

更改控制器输入: 不要期望控制器中出现

ViewModel.Participant
,而是使用更简单的数据类型来接收数据,例如
int ParticipantId

确保选择的唯一性: 确保

<option>
中每个
<select>
的值对于每个参与者都是唯一的。

请随意根据您的代码调整这些建议,以解决潜在问题并确保正确的功能。

添加此型号:

//new model
public class NewParticipantViewModel
{
    public int SelectedParticipantId { get; set; }
}

更换控制器

//my controller:
[HttpPost]
[Route("/Dashboard/postNewBasketballPlayer")]
public IActionResult PostNewBasketballPlayer2(NewParticipantViewModel model)
{
     Console.WriteLine(newParticipant);
     return RedirectToAction("Dashboard","Dashboard");
}

并更改你的cshtml:

//我的cshtml:

@model List<ViewModel.Participant>;

    <form asp-action="PostNewBasketballPlayer" asp-controller="Dashboard" method="POST">
        <div class="form-group">
            <label for="participantSelect">Select Participant:</label>
            <select class="form-control" id="participantSelect" name="SelectedParticipantId">
                @foreach (var participant in Model)
                {
                    <option value="@participant.ParticipantId">
                        @participant.ParticipantFirstName @participant.ParticipantLastName (ID: @participant.ParticipantId)
                    </option>
                }
            </select>
        </div>
        <button type="submit" class="btn btn-primary">Sign up</button>
    </form>
© www.soinside.com 2019 - 2024. All rights reserved.