1

我有一个在我看来已经有效的下拉列表,除了你必须按下提交按钮。有没有办法让它在点击事件上工作,而不是使用提交按钮。我发现有几个人问同样的问题,但对下拉列表使用不同的语法。我是否需要完全改变我的语法,或者我可以添加一些东西来使它与我所拥有的一起工作?

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>

        <legend>Select a State</legend>
    <div>
        @Html.DropDownList("listStates", "Select a State")
    </div>
    <p>

        <input type="submit" value="Submit" />

    </p>

</fieldset>

}

感谢您的链接。这是我使用的:

 public ActionResult Index()
    {

        var query = _db.States.Select(c => new { c.Id, c.Name });
        ViewData["listStates"]  = new SelectList(query.AsEnumerable(), "Id", "Name");

        return View();

    }

剃刀:

@using (Html.BeginForm("Index", "FloorPLan", FormMethod.Post, new { id = "TheForm" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>

        <legend>Select a State</legend>
    <div>
        @Html.DropDownList("Id", (SelectList) ViewData["listStates"], "Select a State", new{
      onchange = "document.getElementById('TheForm').submit();"
   })
    </div>


</fieldset>
4

1 回答 1

0

if you don't mind using jquery, then try the following

$('#Id).on('change', function(){
    $('#TheForm').submit();
});
于 2013-06-08T19:40:07.777 回答