Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何使用带有多个值的@Html.DropDownList_Jquery_Asp.net Mvc - Fatal编程技术网

Jquery 如何使用带有多个值的@Html.DropDownList

Jquery 如何使用带有多个值的@Html.DropDownList,jquery,asp.net-mvc,Jquery,Asp.net Mvc,我有一个列ID、MedicineName和price的table Medicine 现在我有了这个@Html.DropDownList,它由表medicine填充。我想在调用中包含值ID、MedicineName和price,或者在jquery中使用它 目前我只能得到药名 我的控制器: public ActionResult Create() { ViewBag.ClientID = new SelectList(db.Clients, "ClientID", "Sur

我有一个列ID、MedicineName和price的table Medicine

现在我有了这个@Html.DropDownList,它由表medicine填充。我想在调用中包含值ID、MedicineName和price,或者在jquery中使用它

目前我只能得到药名

我的控制器:

 public ActionResult Create()
    {
        ViewBag.ClientID = new SelectList(db.Clients, "ClientID", "Surname");
        ViewBag.MedicineID = new SelectList(db.Medicines, "MedicineID", "MedicineName");
        return View();
    }
我的看法是:

 @Html.DropDownList("MedicineID", ViewBag.MedicineID, htmlAttributes: new { @class = "form-control" })
脚本:

 $("#btnAdd").click(function () {

        var medname = $("#MedicineID option:selected").text()
        var qty = $("#myID").val()

        $("#tblList").append('<tr> <td>' +medname+ '</td>  <td>'+qty+'</td>  <td>   </td>  <td><a class="remove" href="#">del</a></td></tr>')

    })

不能在“选择”列表的每行中存储3个值。它只有Id和文本

您可以做的是一次更改调用webapi,并使用从选择列表中获得的id请求其他数据。

您不能使用@Html.DropDownList,它只允许两个数据值:id和文本

相反,您可以使用@Html.NameFor和@Html.IdFor构建自己的下拉列表:

<select name="@Html.NameFor(Function(model) model.CityId)"
        id="@Html.IdFor(Function(model) model.CityId)">
    @For Each medicine In Model.Medicines
        @<option value="@medicine.MedicineId"
                 data-price="@medicine.price">
            @medicine.MedicineName
        </option>
    Next
</select>

尝试在控制器操作方法的下拉列表中发送格式化数据。您可以使用以下方法:

    public ActionResult Create()
    {
            ViewBag.ClientID = new SelectList(db.Clients, "ClientID", "Surname");

           //modified code
            var medlist = db.Medicines.AsEnumrable().Select(x => new {Id = x.MedicineID, MedName = (x.Id + " "+ x.MedicineName +" "+ x.Price).ToString()});
            ViewBag.MedicineID = new SelectList(medlist, "ID", "MedName");
            return View();
        }
现在,您可以在下拉列表中看到药品的id、名称和价格。您可以根据需要格式化下拉列表。
您可以通过放置适当的字符串函数来提取脚本中的数据。

您可以做的是将ID设置为值,并将MedicineName价格设置为下拉列表中的文本。但这将意味着用户可以在其选择中看到药品名称和价格