由嵌套的DropDownListFor触发的JQuery Postback仅返回第一行

时间:2014-11-12 23:44:35

标签: javascript c# jquery asp.net asp.net-mvc

我有一个对象列表,使用编辑器模板显示。当任务的下拉列表发生更改时,JSON回发始终会返回第一个任务的值,而不管哪个行的下拉列表已更新。

我看到很多相同的问题,但是当触发器位于编辑器模板内的嵌套元素中时却没有。我迷失了我在这里错过的东西。帮助

@model NSCEngineering.Models.task

@Html.HiddenFor(model => model.task_id, new { @id = "taskID" })
@Html.HiddenFor(model => model.task_name, new { @id = "TaskName" })
@Html.HiddenFor(model => model.task_desc, new { @id = "TaskDesc" })

@Html.HiddenFor(model => model.completion_date, new { @id = "CompletionDate" })


<table style="width:80%">
<tr style="width:60%">
    <th colspan="3">@Html.DisplayFor(model => model.task_name)</th>
    <th align="left">
    @Html.DropDownListFor(model => model.task_state_id,
    new SelectList((System.Collections.IEnumerable)ViewData["TaskStates"], "task_state_id", "state"),
                               new { @class = "ddlState"})
    </th>
    <td>
        @Html.EditorFor(model => model.notes, new { @Id = "Notes" })
        @Html.ValidationMessageFor(model => model.notes, "", new { @class = "text-danger" })
    </td>    
</tr>
<tr>
    <td colspan="3">@Html.DisplayFor(model => model.task_desc)</td>
    <td>@Html.Label("Completed by ")@Html.DisplayFor(model => model.user_completed)@Html.Label(", ")@Html.DisplayFor(model => model.completion_date)</td>
</tr>
</table>





   <div class="tasks">
    @Html.EditorFor(m => m.Tasks, new { @class = "Tasks"})   
        </div>
   </div>
}

<p>
    @Html.ActionLink("Back to List", "Index")
  </p>

  @section Scripts{

<script type="text/javascript">
$(this.document).ready(function () {
    $('.ddlState').change(function ()
    {
        var task = {
            "task_id": $("#taskID").val(),
            "task_state_id": $('#ddlState').val(),
            "task_name": $('#TaskName').val(),
            "task_desc": $('#TaskDesc').val(),
            "notes": $('#Notes').val(),
        }
        var url = '@Url.Action("UpdateTaskState")';
        $.ajax({
            type: 'POST',
            url: url,
            data: JSON.stringify(task),
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                alert("success");
                $("#message").html("Success");
                location.reload(true);
            },
            error: function (xhr, status, error) {
                // Show the error
                $('#message').html(xhr.responseText);
            }
        })

    })
});
</script>
}

1 个答案:

答案 0 :(得分:0)

你的代码是:new {@class =&#34; ddlState&#34;})所以你应该使用&#34; task_state_id&#34;:$(&#39; .ddlState&#39;)。val( ),&#34;。&#34;不是&#34; task_state_id&#34;:$(&#39;#ddlState&#39;)。val(),&#34;#&#34;因为它的课程不是id