开发者

Mixing jquery with razor to create dynamicly added selectlists with selected option

I wanna create selectlists with selected option attribute and i wanna increse the value of "i" each time while im creating another selectlist.

var i = -1;
// Hide Section "Loading.gif" On Start 
$(document).ready(function () {
    $("#loading").hide();
    i = i + 1;
    var selects = '@foreach (var selectedone in (System.Collections.IEnumerable)ViewData["Selectlists"]){<tr><td><div id="dive' + i + '"><select name="GroupDetails[' + i + '].cea_id">@foreach(SelectListItem item in (SelectList)ViewData["Suppliers"]){ if(item.Value == selectedone){<option value="@item.Value">@item.Text</option>}else{<option value="@item.Value">@item.Text</option>}}</select>开发者_开发百科;</div></td></tr>}'
    /* alert(selects); */
    $("mytable").append(selects);
});

Just for making it easier to understand the long code up there is this:

     /* im getting selected options as numeric like = "2","8","6" */
@foreach (var selectedone in (System.Collections.IEnumerable)ViewData["Selectlists"])
{
<tr>
    <td>
     /* also in here i need to increase the value of "i" each time! but couldnt embed it yet */
        <div id="dive' + i + '">
            <select name="GroupDetails[' + i + '].cea_id">

                @foreach(SelectListItem item in (SelectList)ViewData["Suppliers"])
                { 
     /* if each items value is equals to selected options that i putted. Do the code below.*/
                    if(item.Value == selectedone)
                    {   
     /* i wanna add this option selected attribute but not working this way */
                        <option selected="selected" value="@item.Value">@item.Text</option>
                    }
                    else
                    {
     /* other options doesnt need to be selected */
                        <option value="@item.Value">@item.Text</option>
                    }
                }
            </select>
        </div>
    </td>
</tr>
}


So the options are rendering but none of them are selected? In that case, maybe your equality comparison is failing? Do the values you're comparing implement Equals correctly? If not, then it will only work if they are exactly the same instance of the same object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜