【问题标题】:Radio button value is not transferred to model - ASP .NET Core单选按钮值未传输到模型 - ASP .NET Core
【发布时间】:2022-01-08 03:43:21
【问题描述】:

我在 asp .net 核心中有一个网页,其中包含以下输入:

@model MyProject.ViewModels.UserScoreModel 
        <div>
            <label asp-for="GivenServiceScore"></label><br />
            <input required asp-for="GivenServiceScore" type="radio" name="servScore" id="servScore" value="5" /> 5
            <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="4" /> 4
            <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="3" /> 3
            <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="2" /> 2
            <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" id="servScore" name="servScore" value="1" /> 1
            <span asp-validation-for="GivenServiceScore" class="text-danger"></span>
        </div>
        <h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
        <div>
            <label asp-for="GivenCommentary"></label><br />
            <textarea class="form-control" asp-for="GivenCommentary"></textarea>
            <span asp-validation-for="GivenCommentary" class="text-danger"></span>
        </div>
        <div class="form-group mb-2" style="padding-top:15px; padding-bottom:15px">
            <button type="submit" class="btn btn-primary">Submit score</button>
        </div>

用户应该给出评分并发表评论。 我的问题是选中的单选按钮中的值不会转移到模型中。当我设置断点并查看参数GivenServiceScore 时,它始终是0。这似乎是一个小问题,但我不明白我错过了什么。
这是模型:

public class UserScoreModel
    {
        [Required]
        [Display(Name = "Please give a rating")]
        public int GivenServiceScore { get; set; }
        [Required]
        [Display(Name = "Please leave a commentary")]
        public String GivenCommentary { get; set; }
    }

还有控制器:

        [HttpPost]
        public async Task<IActionResult> UserReview(UserScoreModel score)
        {
            if(ModelState.IsValid)
            {
                UserReviews review = new UserReviews()
                {
                    GivenServiceScore = score.GivenServiceScore,
                    GivenCommentary = score.GivenCommentary,
                };

                _db.UserReviews.Add(review);
                _db.SaveChanges();

                ViewBag.CongratsMessage = "Thank you for participating!";
                return View();
            }
            return View();
        }

【问题讨论】:

    标签: asp.net-core radio-button


    【解决方案1】:

    使用 asp-for 标签后不需要添加 id 和 name,它会自动生成,如下所示:

    查看:

    @model WebApplication174.Models.UserScoreModel
        <form method="post" asp-action="GivenServiceScore">
            <div>
                <label asp-for="GivenServiceScore"></label><br />
                <input required asp-for="GivenServiceScore" type="radio" value="5" /> 5
                <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="4" /> 4
                <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="3" /> 3
                <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="2" /> 2
                <input required asp-for="GivenServiceScore" type="radio" style="padding-left:15px" value="1" /> 1
                <span asp-validation-for="GivenServiceScore" class="text-danger"></span>
            </div>
            <h5 style="padding-top:15px; padding-bottom:15px">@ViewBag.CongratsMessage</h5>
            <div>
                <label asp-for="GivenCommentary"></label><br />
                <textarea class="form-control" asp-for="GivenCommentary"></textarea>
                <span asp-validation-for="GivenCommentary" class="text-danger"></span>
            </div>
            <div class="form-group mb-2" style="padding-top:15px; padding-bottom:15px">
                <button type="submit" class="btn btn-primary">Submit score</button>
            </div>
        </form>
    

    控制器:

     public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public async Task<IActionResult> GivenServiceScore(UserScoreModel score)
            {
                if (ModelState.IsValid)
                {
                    UserReviews review = new UserReviews()
                    {
                        GivenServiceScore = score.GivenServiceScore,
                        GivenCommentary = score.GivenCommentary,
                    };
    
                    ViewBag.CongratsMessage = "Thank you for participating!";
                    return View();
                }
                return View();
            }
    

    结果:

    【讨论】:

    • 谢谢,我删除了我的自定义 nameid 字段,现在可以使用了!
    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 2018-01-23
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    相关资源
    最近更新 更多