使用javascript填充Razr Select

时间:2018-02-08 21:32:05

标签: javascript c#

我在razor表单中使用javascript填充select元素。在提交表单以确定所选元素时有什么方法吗?我更喜欢在模型的字段上使用razor语法(DisplayFor / EditorFor)。如果我可以& #39; ti将被迫放弃剃刀语法并自动生成所有字段。

的JavaScript

 function fillCombo(comboid,options)
    {
       $.each(options,function(index,elem){
        $("#"+comboid).append(new Option(elem.Name,elem.ID));
       })
}

查看

@using AdminMVC.Models
@using AdminMVC.ConfigConstants
@model AdminMVC.Models.Location

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript" src="~/js/edit.js"></script>
        <script>
            $(document).ready(function(){
                var activities=@Html.Raw(ViewData[Constants.ViewData.TActivities]);
                var comboName="cmbAct";
                fillCombo(comboName,activities);
            });
              </script>
    </head>
    <body>
        <div id="form">

        </div>
        <div id="page">
        @using (Html.BeginForm("Edit","Location",FormMethod.Post))
        {
            <label >Select Activity:</label>
            <div id="cmbActivities">
            **<select id="cmbAct"> //is there any way to get the selected element 
            </select>**
            </div>
            <div id="table">
            <label>Set Location:</label>
            <table border="">

               @Html.HiddenFor(x=>x.Id)
            <tr>
                <td>@Html.DisplayNameFor(x=>Model.Name)</td>
                <td>@Html.EditorFor(x=>Model.Name)</td>
            </tr>
            <tr>
                <td>@Html.DisplayNameFor(x=>Model.Type)</td>
                <td>@Html.EditorFor(x=>Model.Type)</td>
            </tr>
            <tr>
                <td>@Html.DisplayNameFor(x=>Model.Description)</td>
                <td>@Html.EditorFor(x=>Model.Description)</td>
            </tr>

             </table>
             </div>
             <div id="control-panel">
            <input type="submit" value="Edit">
            </div>
        }
    </body>
    </div>
</html>

0 个答案:

没有答案