Convert int to string for dropdownlistfor

I have an int property for Age and a list of selectable possible ages to choose from. When I receive data for editing, I get an error when adding the tostring () method, which says:

Templates can only be used with access to a field, access to properties, an index of a one-dimensional array, or one-parameter expressions of a custom indexer.

so my view model looks like this:

public int LowerAgeLimit { get; set; }
public List<SelectListItem> Ages { get; set; }

My query retrieves the data correctly, and then in the view, I try to display the data as follows:

@Html.DropDownListFor(x => x.LowerAgeLimit.ToString()
                      , Model.CreateGroupForm.Ages
                      , new { @class = "form-control" })

Without the tostring () method, it displays an empty value, which makes sense since it expects a string. But this will not allow me to convert the string without error. I also tried to create a new Age Age property in the viewmodel and convert the int to a string in the request, but I still cannot get a drop-down list to display the value.

In the request, I populated a list of ages as follows:

model.CreateGroupForm.Ages.Insert(0, (new SelectListItem { Text = "", Value = "" }));
        for (int i = 1; i < 100; i++)
        {

            model.CreateGroupForm.Ages.Insert(i, (new SelectListItem {Text = i.ToString(), Value = i.ToString()}));
        }
+4
source share
3 answers

I created a new MVC project and tried the following:
Model:

 public class Test
{

    public int LowerAgeLimit { get; set; }

    public List<SelectListItem> Ages
    {
        get;set;
    }
}

View:

@model WebApplication1.Models.Test
@Html.DropDownListFor(x => x.LowerAgeLimit, Model.Ages)

Controller:

      public ActionResult Index()
    {
        Test tst = new Test();

        tst.Ages = new List<SelectListItem>();
        tst.Ages.Insert(0, (new SelectListItem { Text = "", Value = "" }));
        for (int i = 1; i < 100; i++)
        {
            tst.Ages.Insert(i, new SelectListItem { Text = i.ToString(), Value = i.ToString() });
        }


        return View(tst);
    }

EDIT: , Ages, , , . .
Here is the result

+2

, - . :

:

[Required]
public int? LowerAgeLimit { get; set; }

:

@Html.DropDownListFor(x => x.LowerAgeLimit.ToString()
                  , Model.CreateGroupForm.Ages
                  , new { @class = "form-control" },
                  , "(Choose)")

:

for (int i = 1; i < 100; i++)
{
    model.CreateGroupForm.Ages.Add(new SelectListItem {Text = i.ToString(), Value = i.ToString()});
}

4- DropDownListFor , "dummy", , ( Required , - ).

!

@Html.TextBoxFor(x => x.LowerAgeLimit, 
    new { @class = "form-control", type = "number", min = "1", max = "99"})
0

Try this in your ViewModel:

public int LowerAgeLimit { get; set; }
public SelectList Ages { get; set; }

To fill out, try the following:

var items = new List<SelectListItem>();
items.Insert(0, (new SelectListItem { Text = "", Value = "" }));
for (int i = 1; i < 100; i++)
{

    items.Insert(i, (new SelectListItem {Text = i.ToString(), Value = i.ToString()}));
}

model.CreateGroupForm.Ages = new SelectList (items, "Value", "Text", null);

Your view:

@Html.DropDownListFor(x => x.LowerAgeLimit, (SelectList)Model.CreateGroupForm.Ages, new { @class = "form-control" })
0
source

Source: https://habr.com/ru/post/1625511/


All Articles