简体   繁体   English

使用 Html.RadioButtonFor 通过检查 html 属性

[英]Pass checked html attribute using Html.RadioButtonFor

I want to use @Html.RadioButtonFor(expression, value, htmlAttributes) in ASP.NET MVC with one of the radio buttons checked by default, but it is not working.我想在 ASP.NET MVC 中使用@Html.RadioButtonFor(expression, value, htmlAttributes)并默认选中其中一个单选按钮,但它不起作用。 I've read a lot of posts about this in StackOverflow, but none of them has able to solve the issue.我在 StackOverflow 中阅读了很多关于此的帖子,但没有一个能够解决这个问题。 Any help on how to get this working is appreciated.任何有关如何使这项工作的帮助表示赞赏。 Below is how my MVC looks like:下面是我的 MVC 的样子:

View看法

<h4>Search Book by:</h4>
<span>Title: </span> @Html.RadioButtonFor(expression: m => m.Input.SearchBy,value: "Title", htmlAttributes: new{@checked = true})
<span> | Isbn: </span> @Html.RadioButtonFor(m => m.Input.SearchBy,"Isbn")
<span> | Author: </span> @Html.RadioButtonFor(m => m.Input.SearchBy,"Author")
<span> | Publisher: </span> @Html.RadioButtonFor(m => m.Input.SearchBy,"Publisher")

<button id="FindSubmit" type="submit" class="w-100 btn btn-lg btn-primary">Find</button>

Model Model

public class GetBooksByConditionsParameters : AdminBookParametersBase
    {
        public InputModel Input { get; set; }
        public GetBooksByConditionsParameters() => OrderBy = "title";
        public uint MinPrice { get; set; }
        public uint MaxPrice { get; set; } = 10000;

        public bool ValidPriceRange => MinPrice < MaxPrice;      

        public class InputModel
        {
            public string? SearchTerm { get; set; }
            public string SearchBy = "Title";           
        }            
    }

Controller Controller

[HttpGet]
public IActionResult Find()
{
    return View();           
}

You can try to use @Html.RadioButton to replace @Html.RadioButtonFor :您可以尝试使用@Html.RadioButton替换@Html.RadioButtonFor

<span>Title: </span> @Html.RadioButton("Input.SearchBy", "Title", new { @checked = "checked" })
<span> | Isbn: </span> @Html.RadioButton("Input.SearchBy", "Isbn")
<span> | Author: </span> @Html.RadioButton("Input.SearchBy", "Author")
<span> | Publisher: </span> @Html.RadioButton("Input.SearchBy", "Publisher")

result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM