Home > OS >  Can ASP.NET Core MVC simultaneously use @Html.DisplayFor and @Html.EditorFor?
Can ASP.NET Core MVC simultaneously use @Html.DisplayFor and @Html.EditorFor?

Time:03-31

I'm trying to dip my toe into frontend development with ASP.NET Core 6 MVC hope it looks like this:

enter image description here
because use ASP.NET Web Forms when show data and input text using ajax post backend,

I use data from three models, have two models(STAFF、DEPARTMENT) using show data, make Cascading Dropdown

one model(STAFF_PERMISSION) insert data,make <input type="text">

I use to want it simultaneously @Html.DisplayFor and @Html.EditorFor , but not allowed or only ajax was available

because @Html.EditorFor it can't be found StaffPermission on List<Staff_To_Show>

HomeController.cs


    [HttpGet("[action]")]
    public ActionResult StaffPermission()
    {
        var queryDepar = dbContext.Departments.Join(dbContext.staff,
        o => o.DepartmentNumber, p => p.DepartmentNumber, (o, p) =>
              new Staff_To_Show
              {
                  DepartmentNumber = o.DepartmentNumber,
                  StaffNumber = p.StaffNumber

              }).ToList();

        List<Staff_To_Show> searchStaff = new List<Staff_To_Show>();

        searchStaff.AddRange(queryDepar);

        return View(searchStaff);
    }

    [HttpPost]
    public ActionResult StaffPermission(String DepartmentNumber, String StaffNumber, string AesUpd)
    {

        if (string.IsNullOrWhiteSpace(DepartmentNumber))
        {
            ModelState.AddModelError("Department", "error");
            return View();
        }
        else
        {

            string[] password = getTool.Create(AesUpd);

            StaffPermission addstaff = new StaffPermission();

            addstaff.AesUpd = password[0];
            addstaff.AesKey = password[1];
            addstaff.AesIv = password[2];
            addstaff.LastChangePasswordTime = DateTime.Now;
            addstaff.CreateDate = DateTime.Now;
            addstaff.LastUpd = password[0];
            addstaff.LastKey = password[1];
            addstaff.LastIv = password[2];
            addstaff.IsEnabled = true;
            addstaff.StaffNumber = StaffNumber;
            addstaff.DepartmentNumber = Convert.ToInt16(DepartmentNumber);


            dbContext.StaffPermissions.Add(addstaff);
            dbContext.SaveChanges();

            return View();

        }

    }

StaffPermission.cshtml

@using Voyager.Models.Department
@model List<Staff_To_Show>

@{
    ViewBag.Title = "new";
    
}

<h2>new</h2>

<form action="@Url.Action("StaffPermission")" method="post" action="/Home/StaffPermission">

    @Html.AntiForgeryToken()
  
    <div >

   <select id="DepartmentNumber" name="DepartmentNumber">
            <option value="">select</option>

              @foreach (var item in Model)
    {
           
        <option value="@item.DepartmentNumber"> @Html.DisplayFor(modelItem => item.DepartmentNumber)</option>

    }
           
        </select>
<br/>
   <select id="DepartmentNumber" name="DepartmentNumber">
            <option value="">select</option>

              @foreach (var item in Model)
    {
           
        <option value="@item.StaffNumber"> @Html.DisplayFor(modelItem => item.StaffNumber)</option>

    }
           
        </select>
        
        <hr />
              <div >
            @Html.LabelFor(model => model., htmlAttributes: new { @class = "control-label col-md-2" })
            <div >
                @Html.EditorFor(model => model.StaffPermissions, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StaffPermissions, "", new { @class = "text-danger" })
            </div>
        </div>

 

        <div >
            <div >
                <input type="submit" value="add"  />
            </div>
        </div>
    </div>
  </form>

StaffPermission.cs

using System;
using System.Collections.Generic;

namespace Voyager.Models.Department
{
    public partial class StaffPermission
    {
        public int DepartmentNumber { get; set; }
        public string StaffNumber { get; set; } = null!;
        public string AesUpd { get; set; } = null!;
        public string AesKey { get; set; } = null!;
        public string AesIv { get; set; } = null!;
        public DateTime? CreateDate { get; set; }
        public string? LastUpd { get; set; }
        public string? LastKey { get; set; }
        public string? LastIv { get; set; }
        public DateTime? LastChangePasswordTime { get; set; }
        public bool? IsEnabled { get; set; }
        public virtual staff StaffNumberNavigation { get; set; } = null!;
    }
}

Staff_To_Show.cs

using System;
using System.Collections.Generic;

namespace Voyager.Models.Department
{
    public partial class Staff_To_Show
    {

        public int DepartmentNumber { get; set; }
        public string StaffNumber { get; set; } = null!;
        public IList<StaffPermission> StaffPermissions { get; set; } = null!;

}
}

CodePudding user response:

I suggest you to use ViewModel.

In your situation,change List<Staff_To_Show> into Staff_To_Show.

In Staff_To_Show ,you can change the code like below to add another two model

public class Staff_To_Show
    {
        public List<Department> Departments { get; set; }
         public List<Staff> Staffs { get; set; }
        public StaffPermission StaffPermissions { get; set;}
    }

In StaffPermission.cshtml:

@using Voyager.Models.Department
    @model Staff_To_Show
    
    @{
        ViewBag.Title = "new";
        
    }
    
    <h2>new</h2>
    
    <form  method="post" action="/Home/StaffPermission">
    
        @Html.AntiForgeryToken()
      
        <div >
    
       <select id="DepartmentNumber" name="DepartmentNumber">
                <option value="">select</option>
    
                  @foreach (var item in Model.Departments)
        {
               
            <option value="@item.DepartmentNumber"> @Html.DisplayFor(modelItem => item.DepartmentNumber)</option>
    
        }
               
            </select>
    <br/>
       <select id="DepartmentNumber" name="DepartmentNumber">
                <option value="">select</option>
    
                  @foreach (var item in Model.Staffs)
        {
               
            <option value="@item.StaffNumber"> @Html.DisplayFor(modelItem => item.StaffNumber)</option>
    
        }
               
            </select>
            
            <hr />
                  <div >
                @Html.LabelFor(model => model.StaffPermissions, htmlAttributes: new { @class = "control-label col-md-2" })
                <div >
                    @Html.EditorFor(model => model.StaffPermissions, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.StaffPermissions, "", new { @class = "text-danger" })
                </div>
            </div>
    
     
    
            <div >
                <div >
                    <input type="submit" value="add"  />
                </div>
            </div>
        </div>
      </form>
  • Related