在我的应用中,我总是想要添加一个新的汽车信息到一个特定的表,我必须选择汽车的名称,如果这辆车已经售出,我可以选择它的车主名称。因此,我有新行表的代码:
<td>
@Html.DropDownListFor(model => model.CarName, new SelectList(ViewBag.Cars, "Id", "Description"), new { @class = "iEdit", id = "cars"})
@Html.ValidationMessageFor(model => model.Description)
</td>
<td data-name="carOwners">
@Html.DropDownListFor(model => model.Owner, new SelectList(ViewBag.Owners, "Id", "Description"), new { @class = "iDisable", disabled = "disabled" })
@Html.ValidationMessageFor(model => model.Side)
</td>但是,如何检查汽车是否按第一个@Html.DropDownListFor上选定的值出售,然后启用第二个@Html.DropDownListFor?
为了构建我的ViewBag.Cars,我在我的控制器中使用了以下代码
ICollection<Car> newListCars=new Collection<Car>();
newListCars.Add(new Car(0, "BMW", false));
newListCars.Add(new Car(1, "Toyota", true));
ViewBag.Cars= newListCars;Car.cs:
public class Car
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsSold { get; set; }
public Car(int id, string text, bool sold)
{
Id = id;
Description = text;
IsSold = sold;
}
}所以基本上我想访问所选的ViewBag.Cars.IsSold
发布于 2013-12-12 10:35:48
不必使用dropdownlistfor,您可以通过以下操作创建自己的下拉列表:
<select name="CarName" class="iEdit" id="cars">
@foreach(Car car in ViewBag.Cars)
{
<option value="@car.Id" data-issold="@(car.IsSold ? "true" : "false")">@car.Description</option>
}
</select>然后可以使用所选选项的数据属性,如(jQuery):
var selected = $('#cars').find('option:selected');
var isSold = selected.data('issold'); https://stackoverflow.com/questions/20540768
复制相似问题