0

I have a series of DropDowns that I want the user to add to and edit. I found a helper extension from StackOverflow to build an action image link.

  @Html.DropDownListFor(model => model.Entry.ParadigmId, ((IEnumerable<Pylon.Models.Paradigm>)ViewBag.PossibleParadigms).Select(option => new SelectListItem {
            Text = (option == null ? "None" : option.Name), 
            Value = option.ParadigmId.ToString(),
            Selected = (Model != null) && (option.ParadigmId == Model.Entry.ParadigmId)
        }), "Select")

@Html.ActionImage("ParadigmEdit", new { id = ? }, "~/Content/Images/Edit_Icon.gif", "ParadigmEdit")  

I am not sure how to reference the selected id value in the DropDownList where the question mark is in the above code.

Community
  • 1
  • 1
CyberUnDead
  • 147
  • 5
  • 14

1 Answers1

1

You can't reference the selected value from the dropdown using server side code (which what HTML helpers represent) because the selection is done by the user on the client side. Your problem stems from the fact that you are trying to generate an anchor which should send a value which is known only by the client. You can do this only using javascript. Or another possibility is to simply use a form with an image submit button:

@using (Html.BeginForm("ParadigmEdit", "ControllerName"))
{
    @Html.DropDownListFor(
        model => model.Entry.ParadigmId,
        // WARNING: this code definetely does not belong to a view
        ((IEnumerable<Pylon.Models.Paradigm>)ViewBag.PossibleParadigms).Select(option => new SelectListItem {
            Text = (option == null ? "None" : option.Name), 
            Value = option.ParadigmId.ToString(),
            Selected = (Model != null) && (option.ParadigmId == Model.Entry.ParadigmId)
        }), 
        "Select"
    )
    <input type="image" alt="ParadigmEdit" src="@Url.Content("~/Content/Images/Edit_Icon.gif")" />
}

and of course after you move the ugly code where it belongs (the mapping layer or the view model) your code would become:

@using (Html.BeginForm("ParadigmEdit", "ControllerName"))
{
    @Html.DropDownListFor(
        model => model.Entry.ParadigmId,
        Model.ParadigmValues,
        "Select"
    )
    <input type="image" alt="ParadigmEdit" src="@Url.Content("~/Content/Images/Edit_Icon.gif")" />
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Any pointers on refactoring to a mapping layer or the view model? That is how do I take a Paradigm model class with two properties id and name and convert that into the Model.ParadigmValues? – CyberUnDead Mar 04 '11 at 13:09
  • 1
    @CyberUnDead, Personally I use [AutoMapper](http://automapper.codeplex.com) to convert between my domain models and view models. – Darin Dimitrov Mar 04 '11 at 13:11