ASP.NET Core编辑表单单选按钮回显与修改实现问询
Got it, let's sort out your radio button logic for the edit form. The goal is to pre-select the user's previous choice from the database while letting them update their selection easily. Here's what's off with your current code and how to fix it:
Issues in Your Existing Code
- The
@if (District.Length==(District.Length))check is completely redundant—it will always evaluate totrue, so it doesn't serve any purpose here. - You're hardcoding
checked="checked"andchecked=""instead of dynamically matching against the value from your database (stored inModel.District).
Solution 1: Let Razor Handle It Automatically (Simplest Approach)
Since you're using asp-for="District", Razor can automatically set the checked state based on the value of Model.District. You don't need to write any conditional logic for the checked attribute at all:
@{ // Assuming Model.District holds the user's saved value from the database (e.g., "Apple") } <div class="col-md-10"> <div class="checkbox"> <ul class="list-inline" style="float: right"> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Apple"> Apple </label> </li> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Pear"> Pear </label> </li> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Banana"> Banana </label> </li> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Orange"> Orange </label> </li> </ul> </div> </div>
How this works: When the page renders, Razor checks if Model.District matches the value attribute of the radio button. If it does, it automatically adds the checked attribute for you. This keeps your code clean and avoids manual conditional checks.
Solution 2: Manual Conditional Check (For Fine-Grained Control)
If you prefer explicit control over the checked attribute (maybe for custom logic), you can use Razor's conditional expression to set it dynamically:
@{ var savedDistrict = Model.District; // Store the saved value in a variable for readability } <div class="col-md-10"> <div class="checkbox"> <ul class="list-inline" style="float: right"> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Apple" @(savedDistrict == "Apple" ? "checked" : "")> Apple </label> </li> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Pear" @(savedDistrict == "Pear" ? "checked" : "")> Pear </label> </li> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Banana" @(savedDistrict == "Banana" ? "checked" : "")> Banana </label> </li> <li class="radio space"> <label> <input type="radio" asp-for="District" value="Orange" @(savedDistrict == "Orange" ? "checked" : "")> Orange </label> </li> </ul> </div> </div>
Here, we compare the saved district value against each radio button's value. If they match, we output checked; otherwise, we output nothing. This achieves the same result as the automatic approach but gives you direct control over the attribute.
Extra Tip
I added the option text (like "Apple") inside the <label> tag—this lets users click the text to select the radio button, which is a better user experience than only clicking the button itself.
内容的提问来源于stack exchange,提问作者Assaf Our

