C# 在控制器中写入线的cshtml选项值

C# 在控制器中写入线的cshtml选项值,c#,.net-core,asp.net-core-mvc,C#,.net Core,Asp.net Core Mvc,cshtml @型号列表; @{ 如果(型号!=null) { foreach(模型中的ViewModel.Participant i) { //第11行 @i、 ParticipantFirstName@i.ParticipantLastName id号:@i.ParticipantId } } } 注册 ViewModel.cs using System.Collections.Generic; using System.ComponentM

cshtml

@型号列表;
@{
如果(型号!=null)
{
foreach(模型中的ViewModel.Participant i)
{
//第11行
@i、 ParticipantFirstName@i.ParticipantLastName id号:@i.ParticipantId
}                    
}
}
注册
ViewModel.cs

  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using Microsoft.EntityFrameworkCore;

  namespace LeagueProject.Models
  {
      public class ViewModel
      {
       private Context db;
       public ViewModel(Context context)
       {
           db = context;
       }
       public ViewModel.Participant participant { get; set; }
       public List<Participant> allParticipants { get; set; }
       // List<Participant> allParticipants = db.Participants.Include(i=>i.Parent).ToList();

       public class Participant
       {
           [Key]
           public int ParticipantId { get; set; }

           [Required(ErrorMessage = "is required")]
           [MinLength(2, ErrorMessage = "must be at least 2 characters")]
           public string ParticipantFirstName { get; set; }

           [Required(ErrorMessage = "is required")]
           [MinLength(2, ErrorMessage = "must be at least 2 characters")]
           public string ParticipantLastName { get; set; }

           [Required(ErrorMessage = "is required")]
           public string ParticipantGender { get; set; }

           // [Required(ErrorMessage = "is required")]
           // [Range(8, 20, ErrorMessage="this league if roages 8-19")]
           // public int ParticipantAge { get; set; }

           [Required(ErrorMessage="Need a date of birth")]
           [DataType(DataType.Date)]
           public System.DateTime ParticipantDOB { get; set; }

           public int UserId { get; set; }
           public User Parent { get; set; }

           public List<MMLeagueParticipant> allLeagues { get; set; }
       }
    }
}
我的cshtml在第11行的选项中显示了正确的ParticipantId。所以我知道我在表格中传递的Id是正确的

但当我尝试console.WriteLine在我的仪表板控制器中时,我在命令终端中得到以下信息:

newParticipant
LeagueProject.Models.ViewModel+参与者
newParticipant.ParticipantId
0
纽普
无效的
跳过所有的if检查,因为newP为null
对于我在编写newParticipant.ParticipanId时选择的任何选项,它也会返回0


无论我将什么设置为我的值,我都会在终端中得到相同的响应。

asp.net core使用
name
属性绑定模型数据,尝试将
name
属性添加到您的
选择中,如下所示:

<select name="ParticipantId">
结果:

我写了`Console.WriteLine(newParticipant.participanid);返回重定向到操作(“仪表板”、“仪表板”);`在HttpPost中,但我在终端中得到了LeagueProject.Models.ViewModel+参与者,而不是idNumber。我还将选项更改为value=@i,这样它将返回整个模型。视图窗体中只有一个
?视图中是否有其他代码?
将只返回一个值,它不会将整个模型传递到后端。我只有选择。所以当你呼唤它的时候;您在IActionResult中将其称为对等模型。既然我们不能传递整个模型,它不应该是int吗?你能给我看看你的httpost请求数据吗?
    [HttpPost]
    [Route("postNewBasketballPlayer")]
    public IActionResult PostNewBasketballPlayer(ViewModel.Participant newParticipant)
    {

        if(HttpContext.Session.GetInt32("UserId") == null)
        {
            return RedirectToAction("Index","Home");
        }
        Console.WriteLine("newParticipant");
        Console.WriteLine(newParticipant);

        Console.WriteLine("newParticipant.ParticipantId");
        Console.WriteLine(newParticipant.ParticipantId);

        ViewModel.Participant newP = db.Participants.FirstOrDefault(i=>i.ParticipantId == newParticipant.ParticipantId);
        Console.WriteLine("newP");
        Console.WriteLine(newP);
        if(newP != null)
        {
        // bunch of if checks that have their own returns.
        }
        if(newP == null)
        {
            Console.WriteLine("null");
        }
        Console.WriteLine("skips all if checks because newP was null");
        return RedirectToAction("Dashboard","Dashboard");
    }
<select name="ParticipantId">
@model List<ViewModel.Participant>;
<form asp-action="PostNewBasketballPlayer" 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">
//line 11
                            @i.ParticipantFirstName @i.ParticipantLastName id number: @i.ParticipantId
                        </option>
                    }                    
                }
        }
        </select>
    </div>
    <button class="btn btn-outline-info mt-5" type="submit">Sign up</button>
</form>
[HttpPost]
        [Route("/Dashboard/postNewBasketballPlayer")]
        public IActionResult PostNewBasketballPlayer(Participant newParticipant)
        {

            return Ok();
        }