我一直在使用asp.net MVC,无法在dropdownlistbox中触发更改事件

时间:2010-10-30 05:09:52

标签: asp.net-mvc-2

我已经通过名称AdmissionControlller创建了一个控制器,我添加了两个ActionResult函数:Create和Post Event

// POST: /Admission/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
    try
    {
        // TODO: Add insert logic here
        return RedirectToAction("Index",collection);
    }
    catch
    {
        return View();
    }
}

 //Return the List of Subject For the Class Selected 
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Details(string className)
{
    var subject = SubjectsModel.SelectSubjectsByClass(className);
    return PartialView("EntranceMarks", subject);
}

现在在视图页面

创建页面

<h2>
    Create</h2>
<% using (Html.BeginForm())
{%>
<%= Html.ValidationSummary(true) %>
<fieldset>
    <legend>Fields</legend>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.Id) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.Id) %>
        <%= Html.ValidationMessageFor(model => model.Id) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.PlNo) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.PlNo) %>
        <%= Html.ValidationMessageFor(model => model.PlNo) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.AdmissionNo) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.AdmissionNo) %>
        <%= Html.ValidationMessageFor(model => model.AdmissionNo) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.AdmissionDate) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.AdmissionDate) %>
        <%= Html.ValidationMessageFor(model => model.AdmissionDate) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.FirstName) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.FirstName) %>
        <%= Html.ValidationMessageFor(model => model.FirstName) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.MiddleName) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.MiddleName) %>
        <%= Html.ValidationMessageFor(model => model.MiddleName) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.LastName) %>
    </div>
    <div class="editor-field">
        <%= Html.TextBoxFor(model => model.LastName) %>
        <%= Html.ValidationMessageFor(model => model.LastName) %>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.Class) %>
    </div>
    <%using (Ajax.BeginForm("Details", new AjaxOptions { 
                    UpdateTargetId = "EntranceMarks" }))
    { %>
    <div class="editor-field">
        <%=Html.DropDownList("StudentClass")%>
    </div>
    <div class="editor-label">
        <%= Html.LabelFor(model => model.Section) %>
    </div>
    <div class="editor-field">
        <%=Html.DropDownList("Section")%>
    </div>
    <div class="editor-label" id="EntranceMarks">
    </div>
    <%} %>      
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
<% } %>
<div>
    <%= Html.ActionLink("Back to List", "Index") %>
</div

现在当我更改类的下拉列表框时,我无法触发事件??

1 个答案:

答案 0 :(得分:1)

创建下拉列表时需要包含onchange句柄。下面提到的ChangeClass()方法是您需要创建的javascript函数的名称。

<%=Html.DropDownList("StudentClass", new {onchange="ChangeClass()"})%>

您也可以使用Jquery并将onchange事件绑定到您感兴趣的下拉列表中。

$("#StudentClass").change(function() {
 // your logic goes here
 // if you need to populate some other part of the form
 // this method will probably have some ajax call to a specified
 // controller action which return your data.
});

现在,如果您希望在更改下拉菜单时提交表单,可以使用

来实现
<%=Html.DropDownList("StudentClass", new {onchange="this.form.submit()"})%>