DropDownListFor - does not select “Selected” value

前端 未结 2 694
广开言路
广开言路 2021-01-11 11:14

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

Model:



        
相关标签:
2条回答
  • 2021-01-11 11:21

    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.

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

    0 讨论(0)
  • 2021-01-11 11:28

    When you use @Html.DropDownListFor(m => m.PermissionId, Model.Permissions), please ensure the PermissionId is already set in your Model - that is the selected item / id. In addition, the model contains a 'selected' attribute:

    var query = DatabaseContexts.Select(
                c => new SelectListItem
                {
                    Value = c.Key,
                    Text = c.Value,
                    Selected = c.Key == currentUsed
                });
    
     PermissionId = that want to select;
     Permissions = query.AsEnumerable();
    

    UPDATE:

    here is a full sample of HttpGet:

    In HomeController.cs:

    [HttpGet]
    public ActionResult EditAccount(int id)
    {
        CreateEditAccountModel model = new CreateEditAccountModel();
    
        var permissionsAll = new List<SelectListItem>
             {
                 new SelectListItem
                     {
                         Value = "",
                         Text = "--please select--",
                         Selected = false
                     },
                 new SelectListItem
                     {
                         Value = "1",
                         Text = "Permission one",
                         Selected = true
                     },
                 new SelectListItem
                     {
                         Value = "2",
                         Text = "Permission two",
                         Selected = false
                     }
             };
        model.PermissionId = 1; //add this line
        model.Permissions = permissionsAll; // PermissionsAll();// -- get data from services or anywhere....
        return View("EditAccount", model);
    }
    

    CreateEditAccountModel.cs:

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

    EditAccount.cshtml:

    @using MvcApplication1.Models
    @model CreateEditAccountModel
    @{
       ViewBag.Title = "EditAccount";
     }
    
    <h2>EditAccount</h2>
    @using (Html.BeginForm())
    {
       @Html.DropDownListFor(m => m.PermissionId, Model.Permissions)
    }
    

    Run the application and go to /home/editaccount/1, the dropdown will select "Permission one" by default.

    I think the PermissionId must be type 'string' is, System.Web.Mvc.SelectedListItem.Value is 'string'

    If you would like to do something when user selected item in the dropdown list, you may need [HttpPost].

    BTW: this link is useful to you?

    0 讨论(0)
提交回复
热议问题