简体   繁体   中英

razor inline if statement in javascript

I need to write an inline razor if statement to set selected value of dropdownlist in javascript like this:

$.each(response,
    function (key, value) {
        $(Banner).append($("<option @(dataUi != null && dataUi.Fk_BannerId == value.Id ? "selected=\"selected\"" : "") value="
                        + value.Id + ">"
                        + value.Name + "</option>"));           
    }
);

but is says can not resolve symbol value how can I use value.Id in this razor if statement?

I'm writing this blind so please forgive any typo's.

Try something like this:

<script>
$.each(response, function (key, value) {
    var _dataUiID = "@dataUi.Fk_BannerId";
    var _selected = "";
    if(value.Id == _dataUiID) {
        _selected = "selected='selected'";
    } 
    $(Banner).append($("<option " + _selected + " value="
        + value.Id + ">"
        + value.Name + "</option>"));
    });
</script>

Sorry, not sure if dataUi.Fk_BannerId is numeric or a string so you may have to adjust var _dataUiID = "@dataUi.Fk_BannerId"; a little...also to cater for null ...

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