在表单提交中,仅单选按钮未传递值

时间:2019-11-15 16:12:44

标签: c# html asp.net-core

我在通过单选按钮提交值时遇到一些问题。在按下提交按钮之后,所有值都将传递到相应的属性,但是只有with类型单选按钮不会提交任何值。我正在使用ASP .NET Core 2.2并用C#编写。单选按钮是从此链接中获取的星星:https://codepen.io/jamesbarnett/pen/vlpkh我以前使用过它们,并且可以使用。 这是我的观点:

@model CreateReviewViewModel

@{
    ViewData["Title"] = "Review";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h4>Bar Review</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-controller="Bars" asp-action="Review" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <input asp-for="UserId" value="@User.FindFirstValue(ClaimTypes.NameIdentifier)" hidden />

            <div class="form-group">
                    <fieldset class="rating__stars">
                        <input type="radio" id="star5" asp-for="Rate" name="rating__stars5" value="5" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
                        <input type="radio" id="star4half" asp-for="Rate" name="rating__stars" value="4.5" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
                        <input type="radio" id="star4" asp-for="Rate" name="rating__stars" value="4" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
                        <input type="radio" id="star3half" asp-for="Rate" name="rating__stars" value="3.5" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
                        <input type="radio" id="star3" asp-for="Rate" name="rating__stars" value="3" /><label class="full" for="star3" title="Meh - 3 stars"></label>
                        <input type="radio" id="star2half" asp-for="Rate" name="rating__stars" value="2.5" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
                        <input type="radio" id="star2" asp-for="Rate" name="rating__stars" value="2" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
                        <input type="radio" id="star1half" asp-for="Rate" name="rating__stars" value="1.5" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
                        <input type="radio" id="star1" asp-for="Rate" name="rating__stars" value="1" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
                        <input type="radio" id="starhalf" asp-for="Rate" name="rating__stars" value="0.5" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>
                    </fieldset>
            </div>
            <div class="form-group">
                <br />
                <br />
                <label asp-for="Comment" class="control-label">Message </label>
                <textarea asp-for="Comment" class="form-control" rows="8"></textarea>
                <span asp-validation-for="Comment" class="text-danger"></span>
            </div>
                <input type="submit" value="Create" class="btn btn-primary" />
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

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

这是我的动作:

[HttpPost]
    [Authorize(Roles = "Member")]
    public async Task<IActionResult> Review(CreateReviewViewModel reviewViewModel)
    {
        var barReview = reviewViewModel.ToBarDTO();
        if (ModelState.IsValid)
        {
            await _barManager.CreateBarReviewAsync(barReview);
            return RedirectToAction("Index", "Home");
        }
        return View();
    }

仅提交表单后,完成了CreateViewModel的每个属性(Rate属性除外)。 这是viewModel:

public class CreateReviewViewModel
    {
        public string Id { get; set; }
        public string UserId { get; set; }
        public string Comment { get; set; }
        public decimal Rate { get; set; }
    }

0 个答案:

没有答案