0

I am having a problem getting my drop down list to pick up the correct selected item when I am not working in a webgrid.

So what I have is this:

    @{
    :
    :
        var sql = "select * from AV_SDQ where StudentID = @0 and Assesment_key = @1";
        var scores = db.QuerySingle(sql, StudentID, Assesment_Key);

        var q1 = db.Query("select score_item_enum_key as Col1, abc_value as Col1v from assesment_enum where assesment_key = @0 and seq = 1 order by enum_seq", Assesment_Key)
                            .Select(q1e => new SelectListItem {
                                Value = q1e.Col1.ToString(), 
                                Text = q1e.Col1v.ToString()
                             }); 

        if(IsPost && !Request["buttonSubmit"].IsEmpty()){

           my methods to insert the data -  work beautifully

           scores = db.QuerySingle(sql, StudentID, Assesment_Key);
        }

    }

    <form method="post">
    <table>

    <tr><td>
    Considerate of other people's feelings
    </td><td>
    @Html.DropDownList("Col1", "-- Select --", q1, scores.Col1,  new ***[ WHAT DO I PUT HERE?????????]*** )
    </td></tr>

    </table>


    @{  if(scores != null) {
        if(!IsPost){
            <p><input type="submit" name="buttonSubmit" value="Submit" /></p>
            }
        if(IsPost){
            <p><input type="submit" name="buttonSubmit" value="Submitted!" /></p>
            }

    }}

    </form>

I know the overload for Html.DropDownList takes a fifth argument (new HTML.Attributes) but I am at a total loss of what to put here....... IF I was in a Gridview I would just have new. but it seems that I need new _ and I don't know what to put for ____.

My drop down list is working, and it is setting the value in my DB correctly when I post.

Thanks for your help!

6
  • You can put the css class there like this: new { @class = "myCSSClass" }, or you can use the other overload that only takes 4 arguments if you don't need to put any HTML attributes for your dropdownlist.
    – ekad
    Sep 30, 2014 at 16:22
  • Yeah. There's no overload that requires htmlAttributes to be passed. If you leave it out, it will fallback on the four param overload without it. However, generally speaking, the parameter accepts an anonymous object. Sep 30, 2014 at 16:30
  • The overloads with 4 arguments do not allow you to define the object.SelectedValue those all require 5. Sep 30, 2014 at 16:31
  • Compiler Error Message: CS1031: Type expected Source Error: Line 196:Considerate of other people's feelings Line 197:</td><td> Line 198:@Html.DropDownList("Col1", "-- Select --", q1, scores.Col1, new ( @class = "smallertext") ) Line 199:</td></tr> Line 200:<tr><td> Sep 30, 2014 at 16:31
  • @user3795152 you're not using { and } there, try to change it to new { @class = "smallertext"}.
    – ekad
    Sep 30, 2014 at 16:35

2 Answers 2

0

following up from replies below:

I wanto to set the selectedValue as the 4th parameter. this REQUIRES that I use 5 parameters. I have options of object or IDDictionary htmlAttributes

This is what works:

    </td><td>
    @Html.DropDownList("Col1", "-- Select --", q1, scores.Col1, new { @class = "smallertext"} )
    </td></tr>
0

If you don't need to set any HTML attributes of your dropdownlist, you can just put new { } there

@Html.DropDownList("Col1", "-- Select --", q1, scores.Col1, new { })

However if you need to set one or more HTML attributes of the dropdownlist, here's an example for setting the css class

@Html.DropDownList("Col1", "-- Select --", q1, scores.Col1, new { @class = "smallertext" })

or if there's more than one HTML attributes

@Html.DropDownList("Col1", "-- Select --", q1, scores.Col1, new { @class = "smallertext", @disabled = "disabled" })

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.