简体   繁体   中英

value of selectList from razor in c# Controller

i´m struggling a long time with this problem. I want the value of my selectListItem to use it in the controller to make the right API call.

Thats my Code in razor:

            var selectList = new SelectList(
            new List<SelectListItem>

            {
new SelectListItem {Text = "Borussia Dortmund", Value = "10303", Selected= false},
new SelectListItem {Text = "FC Bayern München", Value = "10285", Selected= false},
new SelectListItem {Text = "Bayer 04 Leverkusen", Value = "10281", Selected= false},
           .
           .
           .
    }, "Value", "Text", 1);
    @Html.DropDownList("ddlChosenTeam", selectList, "Team wählen", new { @class = "css-class" })

}

If i try the following in my Controller, it don´t know the selectList:

protected void SquadChosen_Click(object sender, EventArgs e)
    {
        int selectedSquad = selectList.SelectedValue;

    }

I just want to pick the value like 10303 if "borussia Dortmund" is selected, please safe my time.

Edit: I wrote some other Code that is maybe more what i need. my Controller now:

 public SelectList UserChosenSquad()
    {

        var ssl = new SelectList(new List<SelectListItem>
        {
            new SelectListItem {Text = "Borussia Dortmund", Value = "10303", Selected = false},
            new SelectListItem {Text = "FC Bayern München", Value = "10285", Selected = false},
            new SelectListItem {Text = "Bayer 04 Leverkusen", Value = "10281", Selected = false},

        }, "Value", "Text");


        return ssl;
    }

The Model:

 public class ChosenSquad
{
    public string Text { get; set; }
    public string Value { get; set; }
    public bool Selected { get; set; }

}

now i struggle with the view

@model ChosenSquad

@{ @Html.DropDownListFor(model => model.Text, Model.SelectListItem, "Text", "-Team wählen-")

Is this the right way ? I need to show the DropDownList and then the value like before, i first want to see my list and then perhaps get the value with an if method that looks for selected item, right!?

Although I don't agree with how you're creating this dropdown as you shouldn't really be creating and defining new variables inside the view. To pass the data from the view to the controller you should have an onchange event on the drop down that calls a javascript function.

@Html.DropDownList("ddlChosenTeam", selectList, "Team wählen", new { @class = "css-class", onchange="javascript:sendToController(this.value)" })

and the javascript function should look a bit like this:

function sendToController(value)
{
    var json = '{value: ' + value + '}';

    $.ajax({
       url:'@Url.Action('method','controller')',
       type: 'POST',
       data: json,
       contentType:'Application/json',
       success:function(result){
           //whatever you're doing next
       }
    });
}

Got it. I just edited my List like this:

public List<SelectListItem> ChosenSquad()
    {

        var ssl = new List<SelectListItem>
        {
            new SelectListItem {Text = "Borussia Dortmund", Value = "10303", Selected = false},
            new SelectListItem {Text = "FC Bayern München", Value = "10285", Selected = false},
            new SelectListItem {Text = "Bayer 04 Leverkusen", Value = "10281", Selected = false},

        return ssl;
    }

and in my view i got the following:

<select id=SquadSelectList asp-items="@((List<SelectListItem>)ViewData["Teams"])"></select>

now it renders the List and i just have to get the Value out of it when selected, help would be great but the most important thing is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM