Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Radio button not binding to value

$
0
0

I am using a radio button among selectable option.  Though when I go to an edit view the value of the selected option is not binding to the radio button.  It is possible that I have the code wrong, but I am working it the same way as the checkboxes that I am also using on the page, and they work correctly.

Here is my model that I am working with:

public class FactionViewModel    {        public FactionViewModel()        {            this.FactionNames = new FactionNamesListViewModel();            this.ReputationTypes = new List<ReputationTypeNamesListViewModel>();            this.ReputationLevels = new List<ReputationLevelNamesListViewModel>();        }        public FactionNamesListViewModel FactionNames { get; set; }        [Required]        [Display(Name = "Reputation Type to be associated with Faction")]        public int ReputationTypesID { get; set; }        public List<ReputationTypeNamesListViewModel> ReputationTypes { get; set; }        [Required]        [Display(Name = "Reputation Levels that apply to the Faction")]        public List<ReputationLevelNamesListViewModel> ReputationLevels { get; set; }    }

public class ReputationTypeNamesListViewModel
    {        public int ReputationTypeID { get; set; }        public string ReputationTypeName { get; set; }        public bool IsSelected { get; set; }    }

Here is my controller:

// GET: Factions/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return BadRequest();
            }
            var eFaction = new FactionViewModel()
            {
                FactionNames = await _db.Database.SqlQuery<FactionNamesListViewModel>(sql: "SELECT Faction_ID AS FactionID, Faction_Name AS FactionName FROM Faction WHERE Faction_ID = @p0", parameters: new object[] { id }).FirstOrDefaultAsync(),
                ReputationTypes = await _db.Database.SqlQuery<ReputationTypeNamesListViewModel>(sql: "SELECT Reputation_Type_ID AS ReputationTypeID, Reputation_Type_Name AS ReputationTypeName, CAST (CASE WHEN  Reputation_Type_ID IN (SELECT ReputationTypeReputation_Type_ID FROM Faction WHERE Faction_ID = @p0) THEN 1 ELSE 0 END AS bit) AS IsSelected FROM ReputationType", parameters: new object[] { id }).ToListAsync(),
                ReputationLevels = await _db.Database.SqlQuery<ReputationLevelNamesListViewModel>(sql: "GetRepLevelList @p0", parameters: new object[] { id }).ToListAsync()
            };
            if (eFaction == null)
            {
                return NotFound();
            }

            return View(eFaction);
        }

Here is the view:

@*
    For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_GameDataLayout.cshtml";
}

@model Lotro.WebAppMVC.Models.FactionViewModels.FactionViewModel

<h2>Edit</h2><form asp-controller="Factions" asp-action="Edit" method="post"><div class="form-horizontal"><h4>Class</h4><hr /><div asp-validation-summary="All" class="text-danger"></div><div class="form-group"><label asp-for="@Model.FactionNames.FactionName" class="control-label col-md-2"></label><div class="col-md-10"><input asp-for="@Model.FactionNames.FactionName" class="form-control" /></div></div><div class="form-group"><label asp-for="@Model.ReputationTypesID" class="control-label col-md-12" style="text-align:center"></label><div class="col-md-12">
                @for (var i = 0; i < @Model.ReputationTypes.Count; i++)
                {<div class="col-md-2"><input type="hidden" asp-for="@Model.ReputationTypes[i].ReputationTypeID" /><input type="radio" asp-for="@Model.ReputationTypes[i].IsSelected" value="@Model.ReputationTypes[i].ReputationTypeID"/><b>@Html.DisplayFor(model => model.ReputationTypes[i].ReputationTypeName)</b></div>
                }</div></div><div class="form-group"><label asp-for="@Model.ReputationLevels" class="control-label col-md-12" style="text-align:center"></label><div class="col-md-12">
                @{ int j = 0; }
                @for (var i = 0; i < Model.ReputationLevels.Count; i++)
                {<input type="hidden" asp-for="@Model.ReputationLevels[i].ReputationLevelID" /><div class="col-md-1"><input type="checkbox" asp-for="@Model.ReputationLevels[i].IsChecked" /></div><div class="col-md-2 pull-col-md-1">
                            @Html.DisplayFor(model => model.ReputationLevels[i].ReputationLevelName)</div>
                    j++;
                    if (j == 4)
                    {<div class="col-md-12"><br /></div>
                        j = 0;
                    }
                    else
                    {
                        continue;
                    }
                }</div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-default" /></div></div></div><div><a asp-action="Index">Back to List</a></div></form>

This is what the output of the view is on the page:

<div class="form-group"><label class="control-label col-md-12" style="text-align:center" for="ReputationTypesID">Reputation Type to be associated with Faction</label><div class="col-md-12"><div class="col-md-2"><input name="ReputationTypes[0].ReputationTypeID" id="ReputationTypes_0__ReputationTypeID" type="hidden" value="1" data-val-required="The ReputationTypeID field is required." data-val="true"><input name="ReputationTypes[0].IsSelected" id="ReputationTypes_0__IsSelected" type="radio" value="1" data-val-required="The IsSelected field is required." data-val="true"><b>Crafting</b></div><div class="col-md-2"><input name="ReputationTypes[1].ReputationTypeID" id="ReputationTypes_1__ReputationTypeID" type="hidden" value="2" data-val-required="The ReputationTypeID field is required." data-val="true"><input name="ReputationTypes[1].IsSelected" id="ReputationTypes_1__IsSelected" type="radio" value="2" data-val-required="The IsSelected field is required." data-val="true"><b>Drinking</b></div><div class="col-md-2"><input name="ReputationTypes[2].ReputationTypeID" id="ReputationTypes_2__ReputationTypeID" type="hidden" value="3" data-val-required="The ReputationTypeID field is required." data-val="true"><input name="ReputationTypes[2].IsSelected" id="ReputationTypes_2__IsSelected" type="radio" value="3" data-val-required="The IsSelected field is required." data-val="true"><b>Dwarves</b></div><div class="col-md-2"><input name="ReputationTypes[3].ReputationTypeID" id="ReputationTypes_3__ReputationTypeID" type="hidden" value="4" data-val-required="The ReputationTypeID field is required." data-val="true"><input name="ReputationTypes[3].IsSelected" id="ReputationTypes_3__IsSelected" type="radio" value="4" data-val-required="The IsSelected field is required." data-val="true"><b>Elves</b></div><div class="col-md-2"><input name="ReputationTypes[4].ReputationTypeID" id="ReputationTypes_4__ReputationTypeID" type="hidden" value="5" data-val-required="The ReputationTypeID field is required." data-val="true"><input name="ReputationTypes[4].IsSelected" id="ReputationTypes_4__IsSelected" type="radio" value="5" data-val-required="The IsSelected field is required." data-val="true"><b>Men</b></div><div class="col-md-2"><input name="ReputationTypes[5].ReputationTypeID" id="ReputationTypes_5__ReputationTypeID" type="hidden" value="6" data-val-required="The ReputationTypeID field is required." data-val="true"><input name="ReputationTypes[5].IsSelected" id="ReputationTypes_5__IsSelected" type="radio" value="6" data-val-required="The IsSelected field is required." data-val="true"><b>Other</b></div></div></div>

Also looking at the output from Visual Studio when debugging I see the following:

[0]    {Lotro.WebAppMVC.Models.ReputationTypesViewModel.ReputationTypeNamesListViewModel}
   IsSelected:  false
   ReputationTypeID:  1
   ReputationTypeName: "Crafting"
[1]    {Lotro.WebAppMVC.Models.ReputationTypesViewModel.ReputationTypeNamesListViewModel}
   IsSelected:  true
   ReputationTypeID:  2
   ReputationTypeName: "Drinking"
[2]    {Lotro.WebAppMVC.Models.ReputationTypesViewModel.ReputationTypeNamesListViewModel}
   IsSelected:  false
   ReputationTypeID:  3
   ReputationTypeName: "Dwarves"
[3]    {Lotro.WebAppMVC.Models.ReputationTypesViewModel.ReputationTypeNamesListViewModel}
   IsSelected:  false
   ReputationTypeID:  4
   ReputationTypeName: "Elves"
[4]    {Lotro.WebAppMVC.Models.ReputationTypesViewModel.ReputationTypeNamesListViewModel}
   IsSelected:  false
   ReputationTypeID:  5
   ReputationTypeName: "Men"
[5]    {Lotro.WebAppMVC.Models.ReputationTypesViewModel.ReputationTypeNamesListViewModel}
   IsSelected:  false
   ReputationTypeID:  6
   ReputationTypeName: "Other"

So it is only getting 1 true value.  If I look at the checkboxes on the page they display correctly.  So I am not sure where it is I went wrong, the radio button code and checkbox code should work the same.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>