在 Razor 中使用选择列表

Using SelectList in Razor

我的数据库包含两个 tables ,一个是我的类型,另一个主 table 用于 UI,其中一些数据具有我的类型中的 myid 列。

   myTable
**myid   myname**
1    firstname
2    x-name
99   randomename
 ....

mastertable used in view

id  onecol  twocol refcol <- col is refering mytable
1     xyz    abc   2        <-value 2 is myid of mytable   

..... 鉴于 ,我需要显示所有 myName 而不是 Id,但用 id 更新 master table。所以我设置如下:-

我在 asp.net 中的控制器设置了一些 SelectList 并像

一样传递给了 ViewBag
ViewBag.myType = new SelectList(tableList, "myId", "myName");

在我使用的视图中

<div class="form-group">
    @Html.LabelFor(model => model.myType, new { @class = "control-label col-md-2" })
    <div class="col-md-10">            
        @Html.DropDownList("myType", ViewBag.myType as SelectList,"-Select-", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.myType)
    </div>

以上工作正常但在控制器中我没有得到 myId,并且在传递给控制器​​之前在浏览器中查看时未定义。像下面。我试过了。

在我使用的传递控制器的脚本中

$(".form-group").each(function () {
    if ($(this).find('select').length > 0) {
       attrName = $(this).find('select').attr("name");
        if (attrName == "myType") {       
              //the below is returning undefined 
// the value are undefined for all items as look into chrome debug tool for the list
              //        paramList[attrName] = $(this).find('select').val(); 

//so i used the below that is returning correct text selected
                    var myName = $("#myType option:selected").text();
                    //i need somehwat below, whihc is not correct syntactically
                    var myVal = @{ ((IEnumerable<SelectListItem>)ViewBag.myType).Where(x => x.Value == @:myVal).First();}                        
                    //need to pass value, but is 0 in controller.
                    paramList[attrName] = myVal; 
                }

这不是您问题的答案,但我必须指出这是一个错误:

var myVal = @{ ((IEnumerable<SelectListItem>)ViewBag.myType).Where(x => x.Value == @:myVal).First();}

此代码是 Razor 和 JavaScript 混合在一起的。

注意这是错误的:

JavaScript 是客户端编程语言,而 Razor 是服务器端。

当用户访问您的页面时,您的服务器 (IIS) 将根据您为客户端 return 编码的 Razor 生成一个 HTML 页面。

所以当这个函数在客户端是called/executed时:

$(".form-group").each(function () {

将无法执行此行:

var myVal = @{ ((IEnumerable<SelectListItem>)ViewBag.myType).Where(x => x.Value == @:myVal).First();}

如果你想在客户端访问这个ViewBag.myType,你可以把它转换成JSON:

注意:以下代码尚未经过测试

<script>

var list = @Html.Raw(Json.Encode((IEnumerable<SelectListItem>)ViewBag.myType))

$(".form-group").each(function () {
.....
// Other Code
var myName = $("#myType option:selected").text();

for(var i = 0; i < list.length; i++){   

if(list[i].Text == myName ){


alert('Match Found');

}

}
</script>

总结: 您无法在客户端上使用 JavaScript 执行 Razor 代码。

答案:

你应该做的是 post 列表到 ViewBag

中的视图

控制器:

ViewBag.MydropDown = tableList;

查看:

    @{
        var items = (IEnumerable<myTable>)ViewBag.MydropDown; 
     }

// Note make sure that Property Names are correct in below line  

    @Html.DropDownListFor(m => m.myType, new SelectList(items, "myId","myName"), "--Select--", new{@class = "form-control"})