@ Html.EditorFor(model => model.Username)未显示文本框

时间:2018-07-12 00:31:14

标签: asp.net-mvc entity-framework

我是ASP.NET MVC的新手,正在学习它。我正在使用Asp.Net MVC实体框架。在我称为“注册”的视图中,我正在编写以下代码

<div id="Reg_div"> 
     @using (Html.BeginForm("RegisterUser", "Register", FormMethod.Post))
    {
    <table>

           <tr>

            <td>
                @Html.LabelFor(model => model.Username)
            </td>

            <td>
                @Html.EditorFor(model => model.Username)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.Password)
            </td>
            <td>
                @Html.EditorFor(model => model.Password)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.FName)
            </td>
            <td>
                @Html.EditorFor(model => model.FName)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.LName)
            </td>
            <td>
                @Html.EditorFor(model => model.LName)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.Address)
            </td>
            <td>
                @Html.EditorFor(model => model.Address)
            </td>
        </tr>
        <tr>
            <td>
                @Html.LabelFor(model => model.Email)
            </td>
            <td>
                @Html.EditorFor(model => model.Email)
            </td>
        </tr>

        <tr>

            <td>
                <input type="submit" name="name" value="Register" />
            </td>

        </tr>



    </table>
    }
</div>

enter image description here

这是RegistrationModel.cs

namespace PTMatrix.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

public partial class RegistrationModel : DbContext
{
    public RegistrationModel()
        : base("name=RegistrationModel")
    {
    }

    public virtual DbSet<User_reg> User_reg { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User_reg>()
            .Property(e => e.LName)
            .IsUnicode(false);

        modelBuilder.Entity<User_reg>()
            .Property(e => e.FName)
            .IsUnicode(false);

        modelBuilder.Entity<User_reg>()
            .Property(e => e.Password)
            .IsUnicode(false);

        modelBuilder.Entity<User_reg>()
            .Property(e => e.Address)
            .IsUnicode(false);

        modelBuilder.Entity<User_reg>()
            .Property(e => e.Email)
            .IsUnicode(false);

        modelBuilder.Entity<User_reg>()
            .Property(e => e.Phone)
            .IsUnicode(false);
    }
}

}

User_reg.cs

namespace PTMatrix.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

public partial class User_reg
{
    [Key]
    public int Uid { get; set; }

    [StringLength(50)]
    public string LName { get; set; }

    [StringLength(50)]
    public string FName { get; set; }

    [MaxLength(50)]
    public byte[] Username { get; set; }

    [StringLength(50)]
    public string Password { get; set; }

    [StringLength(50)]
    public string Address { get; set; }

    [StringLength(50)]
    public string Email { get; set; }

    [StringLength(50)]
    public string Phone { get; set; }
}

}

我遇到一个非常奇怪的错误,其中没有显示“用户名”文本框,而是显示了所有其他文本框。我想念什么吗?请帮忙。

1 个答案:

答案 0 :(得分:2)

在您的User_reg中,您将Username定义为byte[],这就是EditorFor不显示文本框的原因, 因为默认情况下,它不知道byte[]的显示内容。

您有两个选择:

  1. byte[]更改为string之类的东西(无论如何,用户名都是byte []很奇怪)
  2. 如果您想要使用byte[],请创建一个EditorTemplate 字节(有关此信息,such as this one上有很多信息) 或使用类似TextboxFor的方式明确指定要输出的内容