C# Html.begin传递模型项值

C# Html.begin传递模型项值,c#,html,asp.net-mvc-4,C#,Html,Asp.net Mvc 4,我使用mvc4,我使用Html.BeginForm调用视图中的另一个控制器 很好用!但是我在这里使用textbox来传递值。 如何修改此代码以便我使用 @DisplayFor(modelItem=>item.UserName) 而不是 @Html.TextBox(“用户名”) 以下是我的看法: 它的形象: @using OTMS.Models @model IEnumerable<OTMS.Models.UserProfile>

我使用mvc4,我使用Html.BeginForm调用视图中的另一个控制器

很好用!但是我在这里使用textbox来传递值。 如何修改此代码以便我使用

@DisplayFor(modelItem=>item.UserName) 而不是 @Html.TextBox(“用户名”)

以下是我的看法:

它的形象:

                     @using OTMS.Models
        @model IEnumerable<OTMS.Models.UserProfile>

        @{
           ViewBag.Title = "Index";
        }

                <!-- Table Continer -->
        <div class="spacer_10px"></div>
        <div class="container clearfix">
            <div class="grid_12"> 
               <div class="table_wrapper table_gray">
        <table>

             <tr>
                <th>
                   <p>User Name</p>
                </th>
                 <th>
                   <p>Role</p>
                </th>
                 <th>
                   <p>Role</p>
                </th>
            </tr>


            @if (Model != null) {
        foreach (var item in Model) {
        <tr>


            <td>
                @Html.DisplayFor(modelItem => item.UserName)
            </td>

            <td>
              @using(Html.BeginForm("GetRoles", "Account",FormMethod.Post)){
              @Html.AntiForgeryToken()
            <div class="editor-label">Username : </div>
             @Html.TextBox("UserName") //here user will enter user name / I dont want user to enter that ,it should be done Automatically 



                  <div class="spacer_20px"></div>
                  <div class="button button-orange"> <span class=" form_button clearfix">
                  <input type="submit" class="submit" name="submit" value="Get Roles for this User" />
                  </span> </div>//by clicking that will pass the user name to controller (GerRole)/I dont want  button
        }


            </td>

            <td>

               @using (Html.BeginForm("Submit", "Account", FormMethod.Post))
           {
                     @Html.Hidden("userName", item.UserName)

                     @Html.DropDownList("selectedRole", (SelectList)ViewBag.Roles)

                     <div class="button button-orange"> <span class=" form_button clearfix">
                    <input type="submit" class="submit" name="submit" value="Update Change" />
                    </span> </div>
            }

            </td>
        </tr>

        }
            }
             </table>
                 </div> </div> 
另一种观点:

它的形象:

@{
ViewBag.Title=“showrole”;
}
扮演
@if(ViewBag.RolesForThisUser!=null){
此用户的角色
@foreach(ViewBag.rolesforthis用户中的字符串s){
  • @s
  • } }
    首先,主要的困惑在于这个助手。请看这里

    • 如果要提供用户不需要知道的发布数据,请使用
      HiddenFor
      。“

    • 当您希望显示记录但不允许编辑记录时,请使用
      DisplayFor

    • 如果要允许用户输入或允许用户编辑字段,请使用
      TextBoxFor
      。 `

    现在你的问题是这样的。。
    如何使用displayfor点击我的控制器!!!!

    您可以使用HiddenFor和DisplayFor来完成此操作。使用HiddenFor可以准备好发布值,而DisplayFor可以显示这些值

    所以要满足你的要求

    <div class="editor-label"> Username : </div>
    @Html.TextBox("UserName") 
    
    用户名:
    @Html.TextBox(“用户名”)
    
    替换

    <div class="editor-label"> Username : </div>
    @Html.HiddenFor(modelItem=>item.username)
    @Html.DisplayFor(modelItem=>item.username) 
    
    用户名:
    @Html.HiddenFor(modeleItem=>item.username)
    @DisplayFor(modelItem=>item.username)
    
    请记住
    Displayfor
    仅在浏览器中呈现标签,若要将其发回控制器,您需要
    HiddenFor

    尝试以下操作:

    控制器 看法
    @ViewBag.Name
    @使用(Html.BeginForm(“GetRoles”、“Account”)){
    @Html.AntiForgeryToken()
    用户名:
    @Html.TextBox(“用户名”)
    }
    

    以下是

    您确实需要做的是为您的视图创建一个视图模型,对我来说,它看起来像这样:

    public class UserViewModel
    {
        public string UserName {get;set;}
        public IEnumerable<string> UserRoles { get; set; }
    }
    
    public ActionResult Index()
    {
        var model = _db.UserProfiles.ToList()
                                    .Select(u => new UserViewModel{
                                         UserName = u.UserName,
                                         UserRoles = Roles.GetRolesForUser(u.UserName)
                                                          .AsEnumerable()
                                    })
                                    .ToList();
        ViewBag.Roles = new SelectList(Roles.GetAllRoles());
        return View(model);
    }
    
    但我不会。这是因为使用这段代码,您为每个用户执行一个传统查询只是为了获取其角色。我认为您需要将角色表添加到EntityFramework模型中,并尝试使用单个查询来执行此操作。因此,您需要使用角色扩展您的UserProfile:

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    
        public ICollection<UserRoles> UserRoles { get; set; }
    }
    
    [Table("webpages_Roles")]
    public class UserRoles
    {
        [Key]
        public int RoleId { get; set; }
        public string RoleName { get; set; }
    
        public ICollection<UserProfile> UserProfiles { get; set; }
    }
    
    在循环中,它将是一个查询,而不是几个查询

    编辑: 您的模型已更改,因此需要将
    @model-IEnumerable
    更改为
    @model-IEnumerable
    然后:

    @foreach(var user in Model)
    {
        //display user
        @foreach(var role in user.UserRoles)
        {
            //display roles with @role
        }
    }
    
    如果您想使用DisplayTemplates,您可以将显示用户的逻辑移动到模板中。为此,您需要按路径创建视图 ~/Views/Shared/DisplayTemplates/UserViewModel.cshtml

    @model OTMS.Models.UserViewModel
    //display user
    @foreach(var role in user.UserRoles)
    {
         //display roles with @role
    }
    
    然后在Index.cshtml中,您可以将代码更改为:

    @foreach (var user in Model)
    {
        @Html.DisplayFor(n => user)
    }
    


    为什么要使用@Html.DisplayFor?为什么不使用@Html.EditorFor?我只想显示值..没有输入字段,没有提交按钮..我不清楚您要归档什么。您想要调用另一个控制器的表单,但不想要提交按钮,但您计划如何将数据发布到控制器?或者使用@DisplayFor(modelItem=>item.UserName)不工作,您想知道如何正确使用它?不,代码工作,请再次查看Q我更新它。我正在使用提交按钮转到另一个视图..我不想在表和同一视图中显示它们,我不知道如何显示!我似乎理解您试图设计的内容。基本上,您想摆脱角色单击此用户按钮,只需使第三列正常工作。当您使用_db.UserProfiles.ToList()查询当前用户时,必须知道当前用户在UserProfile中是否有一个或多个角色?这不是调用我发布的控制器!变量不匹配..将您的变量编辑为控制器中指定的变量我的意思是控制器中的参数是字符串用户名..在我的视图中放在哪里?我更新它Q.我正在使用提交按钮转到另一个视图..我不想在表和同一视图中显示它们,我不知道如何!我只想显示值..没有输入字段,没有提交按钮..通过调用控制器..你的意思是什么,你需要类似于“再次查看Q”的东西吗?我更新它。我正在使用“提交”按钮转到另一个视图..我不想在表和同一视图中显示它们,我不知道怎么做!首先考虑到你解决方案。我如何在视图中显示角色?更新了我的答案。请参阅编辑部分的更改。Oleksii Aza非常感谢,谢谢。我做了第一个解决方案:)……您知道今天只有我真正知道(viewModel)的用途。所以当我想要组合两个表时,它会使用,对吗?一般来说,ViewModel是视图的骨架。它可以是两个表的组合,但有时它可以从一个表中获取数据,并对其进行调整以用于视图。例如:如果您需要只包含用户名的页面,则不必将整个IEnumerable放在视图上。更好的解决方案就是create ViewModel,其中包含IEnumerable并将用户名投影到其中。确定。第三列中的“我的提交”按钮的另一个Q正在更改用户角色,我可以在数据库中看到更改,但单击它将刷新页面(但表将为空!)。我的代码:public ActionResult submit(字符串用户名,字符串selectedRole){//我的逻辑返回视图(“索引”);}
    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    
        public ICollection<UserRoles> UserRoles { get; set; }
    }
    
    [Table("webpages_Roles")]
    public class UserRoles
    {
        [Key]
        public int RoleId { get; set; }
        public string RoleName { get; set; }
    
        public ICollection<UserProfile> UserProfiles { get; set; }
    }
    
    public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }
    
        public DbSet<UserProfile> UserProfiles { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserRoles>()
            .HasMany<UserProfile>(r => r.UserProfiles)
            .WithMany(u => u.UserRoles)
            .Map(m =>
            {
                m.ToTable("webpages_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
        }
    }
    
        var model = _db.UserProfiles.Select(u => new UserViewModel()
        {
            UserName = u.UserName,
            UserRoles = u.UserRoles.Select(ur=>ur.RoleName)
        }).ToList();
    
    @foreach(var user in Model)
    {
        //display user
        @foreach(var role in user.UserRoles)
        {
            //display roles with @role
        }
    }
    
    @model OTMS.Models.UserViewModel
    //display user
    @foreach(var role in user.UserRoles)
    {
         //display roles with @role
    }
    
    @foreach (var user in Model)
    {
        @Html.DisplayFor(n => user)
    }