Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在表单提交中,仅单选按钮不传递值_C#_Html_Asp.net Core - Fatal编程技术网

C# 在表单提交中,仅单选按钮不传递值

C# 在表单提交中,仅单选按钮不传递值,c#,html,asp.net-core,C#,Html,Asp.net Core,我从单选按钮提交值时遇到一些问题。在我按下submit按钮之后,所有值都被传递到相应的属性,但是只有with type单选没有提交任何值。我正在使用ASP.NETCore2.2并用C#编写。单选按钮是我从这个链接中获取的星星:我以前使用过它们,它们工作正常。 我的看法是: @model CreateReviewViewModel @{ ViewData["Title"] = "Review"; Layout = "~/Views/Shared/_Layout.cshtml";

我从单选按钮提交值时遇到一些问题。在我按下submit按钮之后,所有值都被传递到相应的属性,但是只有with type单选没有提交任何值。我正在使用ASP.NETCore2.2并用C#编写。单选按钮是我从这个链接中获取的星星:我以前使用过它们,它们工作正常。 我的看法是:

@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");}
}

HTML表单元素中的“name”属性确定发送到包含字段数据的服务器的参数名称。这必须与模型对象中的名称匹配,以便ASP.NET模型绑定器知道要将值附加到模型的哪个属性。在HTML代码中,您已手动指定
rating\uuuu stars
作为单选按钮的名称(除了其中一个您随机命名的
name=“rating\uu stars5
)。这与模型中的
Rate
属性不匹配。ASP.NET无法神奇地知道您希望这两个字符串代表同一事物。您是否尝试将其作为字符串读取并将其转换为十进制值?您的答案是否正确。仅供参考,您为什么有十进制评级?您似乎希望opti具有整数评级总的说来,谢谢你,阿德森,我以为它应该和这个类的名字是一样的“HTML表单元素中的属性确定发送到包含字段数据的服务器的参数名称。这必须与模型对象中的名称匹配,以便ASP.NET模型绑定器知道要将值附加到模型的哪个属性。在HTML代码中,您已手动指定
rating\uuuu stars
作为单选按钮的名称(除了其中一个您随机命名的
name=“rating\uu stars5
)。这与模型中的
Rate
属性不匹配。ASP.NET无法神奇地知道您希望这两个字符串代表同一事物。您是否尝试将其作为字符串读取并将其转换为十进制值?您的答案是否正确。仅供参考,您为什么有十进制评级?您似乎希望opti具有整数评级总的说来,谢谢你,阿德森,我以为它应该和这个类的名字是一样的。
[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();
    }
public class CreateReviewViewModel
    {
        public string Id { get; set; }
        public string UserId { get; set; }
        public string Comment { get; set; }
        public decimal Rate { get; set; }
    }