admin管理员组

文章数量:1123697

I am binding a radio button to a group in such way that one of them will be isChecked true by using name="someuniquename" but if I do this, the isChecked always comes out false. If I give them different values like asp-for="Phrases[i].IsChecked" name="Phrases[i].IsChecked" it will turn them true but then both radio become clickable. I am trying to assign isChecked=true based on which radio is selected.

I have model:

public class RequestMaintenanceModel
{
    [ValidateNever]
    public int Id { get; set; }
    
    [ValidateNever]
    public List<PhraseModel> phraseModel { get; set; }
    public List<PhraseModel> Phrases { get; set; } = PhraseModel.PredefinedPhrases();
    
}

And phrases hardcoded at the beginning:

public class PhraseModel
{
    [ValidateNever]
    public int Id { get; set; }
    public string Phrase { get; set; }
    public string Category { get; set; }
    public bool IsChecked { get; set; }
    [ValidateNever]
    [ForeignKey("RequestMaintenanceModel")]
    public int RequestMaintenanceID { get; set; }
    public RequestMaintenanceModel RequestMaintenance { get; set; }



    public static List<PhraseModel> PredefinedPhrases()
    {
        var phrases = new List<PhraseModel>
        {
            new PhraseModel{Id=1, Category="Request Status", Phrase="Normal", IsChecked=false},
            new PhraseModel{Id=2, Category="Request Status", Phrase="Urgent", IsChecked=false},
            
        };
        return phrases;
    }
}

Controller:

public IActionResult Create()
{
    var model = new RequestMaintenanceModel();
    
    return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(RequestMaintenanceModel model)
{
    if (model.Phrases != null)
    {
       
    if (ModelState.IsValid)
    {
      
        _context.requestMaintenanceModel.Add(model);
        await _context.SaveChangesAsync();
        if (model.Phrases != null && model.Phrases.Any())
        {
            var selectedPhrases = model.Phrases.Where(p => p.IsChecked).ToList();
            foreach (var phrase in selectedPhrases)
            {
                phrase.RequestMaintenanceID = model.Id;
                _context.phraseModel.Add(phrase);
            }
        }
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");

        
        
    }
    return View("Create", model);
}

View:

@for (int i = 0; i < Model.Phrases.Count; i++)
{
    if (Model.Phrases[i].Category == "Request Status")
    {
        <div class="form-check form-check-inline">
            <input class="form-check-input"
                   type="radio"
                   asp-for="Phrases[i].IsChecked"
                   name="RequestStatus"
                   value="true"
                   id="Phrase_@i" />
            <label class="form-check-label" for="Phrase_@i">@Model.Phrases[i].Phrase</label>
        </div>
        <input type="hidden" name="Phrases[@i].Id" value="@Model.Phrases[i].Id" />
        <input type="hidden" name="Phrases[@i].Category" value="@Model.Phrases[i].Category" />
        <input type="hidden" name="Phrases[@i].Phrase" value="@Model.Phrases[i].Phrase" />
        <input type="hidden" name="Phrases[@i].IsChecked" value="false" />
    }
    <!-- Hidden inputs to preserve other properties -->
    
}

I am binding a radio button to a group in such way that one of them will be isChecked true by using name="someuniquename" but if I do this, the isChecked always comes out false. If I give them different values like asp-for="Phrases[i].IsChecked" name="Phrases[i].IsChecked" it will turn them true but then both radio become clickable. I am trying to assign isChecked=true based on which radio is selected.

I have model:

public class RequestMaintenanceModel
{
    [ValidateNever]
    public int Id { get; set; }
    
    [ValidateNever]
    public List<PhraseModel> phraseModel { get; set; }
    public List<PhraseModel> Phrases { get; set; } = PhraseModel.PredefinedPhrases();
    
}

And phrases hardcoded at the beginning:

public class PhraseModel
{
    [ValidateNever]
    public int Id { get; set; }
    public string Phrase { get; set; }
    public string Category { get; set; }
    public bool IsChecked { get; set; }
    [ValidateNever]
    [ForeignKey("RequestMaintenanceModel")]
    public int RequestMaintenanceID { get; set; }
    public RequestMaintenanceModel RequestMaintenance { get; set; }



    public static List<PhraseModel> PredefinedPhrases()
    {
        var phrases = new List<PhraseModel>
        {
            new PhraseModel{Id=1, Category="Request Status", Phrase="Normal", IsChecked=false},
            new PhraseModel{Id=2, Category="Request Status", Phrase="Urgent", IsChecked=false},
            
        };
        return phrases;
    }
}

Controller:

public IActionResult Create()
{
    var model = new RequestMaintenanceModel();
    
    return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(RequestMaintenanceModel model)
{
    if (model.Phrases != null)
    {
       
    if (ModelState.IsValid)
    {
      
        _context.requestMaintenanceModel.Add(model);
        await _context.SaveChangesAsync();
        if (model.Phrases != null && model.Phrases.Any())
        {
            var selectedPhrases = model.Phrases.Where(p => p.IsChecked).ToList();
            foreach (var phrase in selectedPhrases)
            {
                phrase.RequestMaintenanceID = model.Id;
                _context.phraseModel.Add(phrase);
            }
        }
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");

        
        
    }
    return View("Create", model);
}

View:

@for (int i = 0; i < Model.Phrases.Count; i++)
{
    if (Model.Phrases[i].Category == "Request Status")
    {
        <div class="form-check form-check-inline">
            <input class="form-check-input"
                   type="radio"
                   asp-for="Phrases[i].IsChecked"
                   name="RequestStatus"
                   value="true"
                   id="Phrase_@i" />
            <label class="form-check-label" for="Phrase_@i">@Model.Phrases[i].Phrase</label>
        </div>
        <input type="hidden" name="Phrases[@i].Id" value="@Model.Phrases[i].Id" />
        <input type="hidden" name="Phrases[@i].Category" value="@Model.Phrases[i].Category" />
        <input type="hidden" name="Phrases[@i].Phrase" value="@Model.Phrases[i].Phrase" />
        <input type="hidden" name="Phrases[@i].IsChecked" value="false" />
    }
    <!-- Hidden inputs to preserve other properties -->
    
}
Share Improve this question edited 23 hours ago Ruikai Feng 11.3k1 gold badge6 silver badges16 bronze badges asked yesterday awaisharoonawaisharoon 5031 gold badge4 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Modify your models:

public class RequestMaintenanceModel
 {
     [ValidateNever]
     public int Id { get; set; }

     [ValidateNever]
     public List<PhraseModel> phraseModel { get; set; }
     public List<PhraseModel> Phrases { get; set; } = PhraseModel.PredefinedPhrases();


     public string ? Phrase { get;set; }

 }




 public class PhraseModel
 {
     [ValidateNever]
     public int Id { get; set; }
     public string Phrase { get; set; }
     public string Category { get; set; }
     //public bool IsChecked { get; set; }
     [ValidateNever]
     [ForeignKey("RequestMaintenanceModel")]
     public int RequestMaintenanceID { get; set; }
     public RequestMaintenanceModel RequestMaintenance { get; set; }



     public static List<PhraseModel> PredefinedPhrases()
     {
         var phrases = new List<PhraseModel>
     {
         new PhraseModel{Id=1, Category="Request Status", Phrase="Normal"},
         new PhraseModel{Id=2, Category="Request Status", Phrase="Urgent"},

     };
         return phrases;
     }
 }

View:

@model RequestMaintenanceModel

<form method="post">

   
    @for (int i = 0; i < Model.Phrases.Count; i++)
    {
        if (Model.Phrases[i].Category == "Request Status")
        {
            <div class="form-check form-check-inline">
                <input class="form-check-input"
                       type="radio"
                       asp-for="Phrase"
                       value="@Model.Phrases[i].Phrase" />
                <label class="form-check-label" for="Phrase_@i">@Model.Phrases[i].Phrase</label>
            </div>
            <input type="hidden" name="Phrases[@i].Id" value="@Model.Phrases[i].Id" />
            <input type="hidden" name="Phrases[@i].Category" value="@Model.Phrases[i].Category" />
            <input type="hidden" name="Phrases[@i].Phrase" value="@Model.Phrases[i].Phrase" />
            
        }
        <!-- Hidden inputs to preserve other properties -->
    }
    <button type="submit"> Submit</button>
</form>

Result:

When the post request is done, the radio buttons won't be both clickable

本文标签: cBind a radio button group to model in NetStack Overflow