0

I am using MVC Razor View.

At the top of my form I have the following:

@using (Html.BeginForm())

it appears that none of my checkboxes or radiobuttons, or dropdown are working, they are all returning a value of null

checkbox : the model.Domestic is a boolean, could this be an issue? returns null

  @Html.CheckBoxFor(model => model.Domestic.Value, new { value = Model.Domestic.Value ? "checked" : "" })&nbsp;&nbsp;<span class="display-checkbox">Domestic</span>

radio button returns null

@Html.RadioButtonFor(model => model.Subscription_Renewal, false, new { id = "SR_ChkNo", value = "checked", onclick = "checkButtonDt();" })&nbsp;&nbsp;&nbsp;<span class="largeText_light">N/A</span>

dropdown:

backend code:

 // Create the Categories Select List 
                var categoryList = new List<SelectListItem>();
                foreach (Category c in returned.Categories)
                {
                    categoryList.Add(new SelectListItem
                    {
                        Value = c.Category_ID.ToString(),
                        Text = c.Description,
                        Selected = (c.Category_ID == returned.Category_ID ? true : false)

                    });
                }

                returned.CategoryList = categoryList;

then in the View: I tried to select a entry from the selected list, and it returns 0

@Html.DropDownList("category", Model.CategoryList, new { @class = "input-box" })

1 Answer 1

1

Here is a demo to bind data with checkbox,radiobutton and dropdownlist when posting form:

Model:

public class BindModel 
    {
        public List<SelectListItem> CategoryList { get; set; }
        public string Subscription_Renewal { get; set; }
        public bool Domestic { get; set; }
        public string category { get; set; }


    }

View:

@using (Html.BeginForm())
{
    @Html.CheckBoxFor(model => model.Domestic)<span class="display-checkbox">Domestic</span>
    <input type="radio" name="Subscription_Renewal" value="Male" /><span class="largeText_light">Male</span>
    <input type="radio" name="Subscription_Renewal" value="Female" /><span class="largeText_light">Female</span>
    <input type="radio" name="Subscription_Renewal" value="Unspecified" /><span class="largeText_light">Unspecified</span>
    @Html.DropDownList("category", Model.CategoryList, new { @class = "input-box" })
    <input type="submit" value="submit" />
}

Controller:

public IActionResult TestBind(BindModel bindModel) {
            BindModel returned = new BindModel();
            var categoryList = new List<SelectListItem> { 
                new SelectListItem {  Value="1", Text="Category1",Selected=true}, 
                new SelectListItem { Value = "2", Text = "Category2", Selected = false }, 
                new SelectListItem { Value = "3", Text = "Category3", Selected = false } };
            

            returned.CategoryList = categoryList;
            return View(returned);
        }

result: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for you assistance here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.