简体   繁体   中英

Pass html.dropdownlistfor current value to Actionlink?

is it possible to pass @Html.DropDownListFor current value to action link? I want to pass the template value to the Sample controller with Create Action. Below code not work because @Model.SurveyTemplate does not return any value. Is it need JavaScript to retrieve? Please guide me.

Drop Down List:

@Html.LabelFor(model => model.Survey_Template, "Survey Template")
                @Html.DropDownListFor(model => model.Survey_Template, new SelectList(
                  new List<Object>{
                       new { value = "Style1" , text = "Style1"  },
                       new { value = "Style2" , text = "Style2" },
                       new { value = "Style3" , text = "Style3"}
                    },
                  "value",
                  "text",
            0))

ActionLink:

@Html.ActionLink("_", "Create", "Sample",
            new { template = @Model.Survey_Template },
            new { @id = "popup-link", @class = "classname2" })

You have to do this with JavaScript , the razor syntax is recognized only on server side . This means that @Model.Survey_Template will be rendered when user request the page and will have the default value.

If you choose to do it with JavaScript remember that you need to save the base url and to append parameters to it based on what has been selected.

This steps will help you to achieve your desired result:

  1. Attach a change event on your dropdown.
  2. Get the selected value and construct the new url
  3. Replace the url ( href ) of the action ( <a .. /> )

Note: I didn't write the code because I want you to surpass yourself.


I think what you try to achive it's a form with GET .

@using(Html.BeginForm("Create", "Sample", FormMethod.GET)){
    @Html.LabelFor(model => model.Survey_Template, "Survey Template")
    @Html.DropDownListFor(model => model.Survey_Template, new SelectList(
        new List<Object>{
           new { value = "Style1" , text = "Style1"  },
           new { value = "Style2" , text = "Style2" },
           new { value = "Style3" , text = "Style3"}
        },
        "value",
        "text",
    0))

    <input type="submit" value="_" class="classname2" id="popup-link" />
}

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