0

// 此代码抛出一个错误,即没有类型的视图数据项 //IEnumerable for Companytype

//RegisterModel 中的当前代码

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Email Address")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Company Name")]
    public string CompName { get; set; }


    [Required]
    [Display(Name = "Company Type")]
    public IEnumerable<SelectListItem> CompTypeList { get; set; }



    [Required]
    [Display(Name = "Total number of Branches")]
    [Range(1,10,ErrorMessage = "The {0} must be at least {1}")]
    public int TotalBranches { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }






}

//当前代码在视图中

        <li>
            @Html.LabelFor(m => m.UserName)
            @Html.TextBoxFor(m => m.UserName)
        </li>

        <li>
            @Html.LabelFor(m => m.Email)
            @Html.TextBoxFor(m => m.Email)
        </li>

        <li>
            @Html.LabelFor(m => m.CompName)
            @Html.TextBoxFor(m => m.CompName)
        </li>

        <li>
            @Html.LabelFor(m => m.CompTypeList)
            @Html.DropDownList("Companytype","-select one-")
        </li>

        <li>
            @Html.LabelFor(m => m.TotalBranches)
            @Html.TextBoxFor(m => m.TotalBranches)
        </li>




        <li>
            @Html.LabelFor(m => m.Password)
            @Html.PasswordFor(m => m.Password)
        </li>
        <li>
            @Html.LabelFor(m => m.ConfirmPassword)
            @Html.PasswordFor(m => m.ConfirmPassword)
        </li>

//AccountController 注册私有 DefaultDBEntities3 db = new DefaultDBEntities3();

    [AllowAnonymous]
    public ActionResult Register()
    {
        ViewBag.CompTypeList = new SelectList( "Companytype");
        return View();
    }

    protected override void Dispose(bool disposing)
    {

        db.Dispose();
        base.Dispose(disposing);
    }  

您的帮助将不胜感激

4

1 回答 1

0

选择列表项是 html.dropdownlist 帮助器的无效参数。它正在寻找一个列表。仅将数据存储在 viewbag 中,并在视图中创建选择列表,如下所示:

//控制器

public ActionResult Register()
{
ViewBag.CompTypeList = db.Companytype.OrderBy(y => y.Name).ToList();
return View();
}

//看法

@Html.DropDownList("CompanyType", new SelectList(ViewBag.CompTypeList, "Id", "Name"))

这将在视图中正确呈现您的下拉列表并消除您的错误。Id 是列表的值字段,Name 是要显示的字段。希望这可以帮助。

于 2013-04-01T03:27:07.497 回答