0
public class TestViewModel
{
    private IUserRepository _userRepository = new UserRepository();

    public int UserID { get; set; }

    public IEnumerable<SelectListItem> users
    {
        get
        {
            var result =  _userRepository.GetAllUsers()
                .Select(x => new SelectListItem
                {
                   Value = x.UserID,
                   Text = x.Name
                });
            return result;
        }
    }
}

测试视图.cshtml

@model TestNamespace.TestViewModel

<h2>Test</h2>
@Html.DropDownListFor(x =>x.UserID,
    new SelectList(Model.users,"Value","Text"), "--Select User--"       
)

当我在下拉列表中选择值时,该值没有被选中。我写了一个.change()没有被提出的jquery。$("#UserID").change();

它显示"The field UserID must be a number"

<script type="text/javascript">

$(document).ready(function () {

    $("#UserID").change(function () {
        alert("Test");
    });
});

4

0 回答 0