我注意到在我看来asp.net MVC中的一个错误,或者只是我做错了什么.我目前正在使用1.0,所以这可能会在2.0版本中得到解决.但不管怎样,我们走了.
当我的视图模型具有与下拉列表的声明ID同名的属性时,将忽略所选项,并且渲染的html没有选择任何内容.不确定我是否做错了,但更改ID的名称可以解决问题.我简化了这个例子,希望很清楚,否则请告诉我.
这是我的视图,其中声明的ID与模型中的列表名称相同:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("IsMultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
和渲染的Html
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<select id="IsMultipleServicers" name="IsMultipleServicers">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
现在让我们做一个小改动.我将更改声明的id为不同的东西.
这是我的观点:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<%= Html.DropDownList("MultipleServicers", Model.IsMultipleServicers) %>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
现在渲染的html:
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<select id="IsMultipleServicers" name="IsMultipleServicers">
<option value="false">No</option>
<option selected="selected" value="true">Yes</option>
</select>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
请注意,现在我得到一个选定的选项,它将是List中的第二个元素.
这是我的ViewModel只是将所有内容联系在一起:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using …Run Code Online (Sandbox Code Playgroud)