DropDownListFor - 不选取[选择"值 [英] DropDownListFor - does not select "Selected" value

查看:231
本文介绍了DropDownListFor - 不选取[选择"值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 DropDownListFor 不是选择的选定的值,这些问题一个又一个。
这里是code:

Another one of these questions regarding DropDownListFor not selecting the "Selected" value. Here is code:

型号:

public class CreateEditAccountModel
{
    [Required]
    [Display(Name = "Permission")]
    public int PermissionId { get; set; }
    public IEnumerable<SelectListItem> Permissions { get; set; }
}

控制器:

[HttpGet]
[Authorize]
public ActionResult EditAccount(int id)
{
    CreateEditAccountModel model = new CreateEditAccountModel();
    model.Permissions = PermissionsAll();
    return View("CreateEditAccount", model);
}

在这一点上,如果我把一个破发点上回线, model.Permissions 包含适当的的IEnumerable&LT; SelectListItem&GT; 与多个项目,只有一个对象,有选择= TRUE

At this point if I put a break point on return line, the model.Permissions contains proper IEnumerable<SelectListItem> object with multiple items and only one, that has Selected = true.

查看:

@using (Html.BeginForm())
{
    @Html.DropDownListFor(m => m.PermissionId, Model.Permissions)
}

渲染:

<select id="PermissionId" name="PermissionId">
    <option value="">-- Select --</option>
    <option value="1">Permission one</option>
    <option value="2">Permission two</option>
</select>

由于某些原因没有被选择的属性上的任何选项,并选中第一个选项。

For some reason there is no selected attribute on any of the options and first option is selected.

任何帮助是AP preciated。

Any help is appreciated.

更新

看起来它是与此<一个href=\"http://publicityson.blogspot.com/2010/07/aspnet-mvc-htmldropdownlist-not-showing.html\">article.总结这篇文章的解决方案,我需要确保该属性的名称(第一个参数 @ html.DropDownList )的匹配任何该模型的现有属性。的任何人能解释为什么是这样?

It appears it has something to do with this article. To summarize the solution of this article, I need to make sure that the property name (first argument of @html.DropDownList) does not match any existing properties of the model. Can anybody explain why this is the case?

这使得当我的观点是这样写的下拉正确:

It renders drop down correctly when I write in the view something like this:

@Html.DropDownList("PermissionIdNotMatching", Model.Permissions)

然而,它并不因为其实我是想粘结剂能够匹配选择元素到模型属性的名称作任何逻辑意义去做。否则,我就不得不手动抓住这样的值:的Request.Form [PermissionIdNotMatching];

任何人有任何想法?

SOLUTION

请参阅接受的答案,并给它的第一个评论。

See accepted answer and the first comment to it.

推荐答案

好吧,让你的例子来讨论,当 PermissionId 是int类型。你贴键入 CreateEditAccountModel 来查看模式。当你创建了这个模型, PermissionId 等于0(INT的默认值)。而鉴于 DropDownListFor 得到这个值。因此,你没有选择的值。
当您使用的字符串类型,默认值 PermissionId 为空,的SelectList的`因此采取DropDownListFor默认值。

Ok, let's discuss about your example, when PermissionId was int. You posted model of type CreateEditAccountModel to view. When you created this model, PermissionId equal 0 (default value of int). And DropDownListFor in view get this value. Therefore you had no selected values. When you used string type, default value of PermissionId was null, therefore `DropDownListFor taken default value of SelectList.

在这种情况下,最好使用 INT 可空&LT; INT&GT; 键入 PermissionId

In this case it's better to use int? or Nullable<int> type for PermissionId.

这篇关于DropDownListFor - 不选取[选择&QUOT;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆