-1

如果可能,请在此处提供帮助或以其他方式建议我

C#代码如下

 public ActionResult Create()
 {
        var newCircuit = new CircuitViewModel();
        newCircuit.Workouts = _db.Workouts.ToList();
        newCircuit.MemberId = _memberId;
        return View(newCircuit);
}

在 cshtml 页面上的代码是

@Html.DropDownList("ddlWorkout", new SelectList(Model.Workouts, "Id", "Name"), "--Select Workout--", new { required = true, style = "width:310px", onchange = "GetExercise(this)" })

jQuery代码如下:

function GetExercise() {     
    var Workouts = @Model.Workouts
    var AllExercises = Workouts[0].Exercises; 
}

基本上在模型中我有锻炼,并且在每次锻炼下都有多个锻炼,所以当我选择锻炼时,我想显示所有锻炼,如果我选择另一个锻炼,那么它的相关锻炼,等等......

所以基本上我想@Model.Workouts在 jQuery Workouts 变量中设置。但上面的 jQuery 代码对我不起作用。

4

2 回答 2

0

根据你的例子使用这个:

@Html.DropDownList("ddlWorkout", new SelectList(Model.Workouts, "Id", "Name"), "--Select Workout--", new { required = true, style = "width:310px", onchange = "GetExercise($(this))" })

function GetExercise($(this)) {     
var Workouts = $(this.val())
var AllExercises = Workouts[0].Exercises; 
}
于 2012-11-07T07:50:55.030 回答
0

从上面我了解到,您需要 Js 中的锻炼 ID 下拉列表,以便您可以根据锻炼 ID 绑定锻炼结果。这样做:

@Html.DropDownList("ddlWorkout", new SelectList(Model.Workouts, "Id", "Name"), "--Select Workout--", new { required = true, style = "width:310px", onchange = "GetExercise(this)" })
function GetExercise(obj) {     
    // First i'm checking whether user has selected any value in dropdown or not
    if(obj!="" || obj.val()>0)
    {
        var Workouts = obj.val(); //Will have dropdown value
        var AllExercises = Workouts[0].Exercises; //This line i did not get
    } 
}

或者这样做

//Set id from dropdown
@Html.DropDownList("ddlWorkout", new SelectList(Model.Workouts, "Id", "Name"), "--Select Workout--", new { required = true, style = "width:310px", @id= "ddlWorkout" })

$(function() {   
    $("#ddlWorkout").change(function() {
         var obj = $("#ddlWorkout").val();
        // First i'm checking whether user has selected any value in dropdown or not
        if(obj != "" || obj>0)
        {
            var Workouts = obj.val(); //Will have dropdown value
           var AllExercises = //Pass Exercise id to get result here
    } 
}
于 2016-02-04T07:49:31.453 回答