The model item passed into the ViewDataDictionary is of type 'EntityCrudIdentity.ViewModels.MemberViewModel', but this ViewDataDictionary instance requires a model item of type 'EntityCrudIdentity.ViewModels.EditMemberViewModel'.

Tabish Khan 1 Reputation point
2022-06-06T12:20:14.03+00:00

Controller-->
[HttpGet]
public async Task<IActionResult> User1(string Username)
{
var std = await student.GetStudentUserName(Username);
MemberViewModel member = new MemberViewModel()
{

                        UserName = std.UserName,  
                        Email = std.Email,  
                        PhoneNo = std.PhoneNo  
                    };  
          
          
            member.ProtectedId = protector.Protect(member.UserName.ToString());  
          


        return View(member);  
    }  
     [HttpGet]  
    public async Task<IActionResult> Edit(string EId)  
    {  
        var idint = protector.Unprotect(EId);  
        var std = await student.GetStudentUserName(idint);  
        std.ProtectedId = EId;  
        return View(std);  
    }  

Interface-->

public async Task <MemberViewModel> GetStudentUserName(string em)
{
IdentityUser user = await userManager.FindByNameAsync(em);
MemberViewModel member = new MemberViewModel()
{

            UserName = user.UserName,  
            Email = user.Email,  
            PhoneNo = user.PhoneNumber  
               
        };  
        return member;  

    }  

View-->User

@using EntityCrudIdentity.ViewModels

@默 MemberViewModel

@{
ViewData["Title"] = "List";
}

<h1>List</h1>
<table class="table table-bordered ">
<thead class=table-dark>
<tr class="text-center">
<th>UserName</th>
<th>PhoneNo</th>
<th>Email</th>

            <th>Edit</th>  
            <th>Delete</th>  
        </tr>  
    </thead>  
    <tbody>  
        <tr class ="text-center">    
            <td>@Model.UserName</td>  
              
            <td>@Model.PhoneNo</td>  
            
            <td>@Model.Email</td>  
             
            <td>  
               <a class="btn btn-primary" title= "Edit"  asp-controller="Login" asp-action="Edit" asp-route-EId="@Model.ProtectedId">Edit<i class="fa-solid fa-pen-to-square"></i></a>   
            </td>  
            @*<td>  
                 <form method="post">  
                    <button class="btn  btn-danger" title="Delete" type="submit" onclick="return confirm('Are you sure want to delete')"   asp-controller="Student" asp-action="Delete" asp-route-DId=@item.PtotectedId>Delete<i class="fa-solid fa-trash"></i></button>    
                </form>  
            </td>*@  
        </tr>  
          
       
    </tbody>     
</table>  

View--> Edit

@using EntityCrudIdentity.ViewModels

@默 EditMemberViewModel
<h1>Edit</h1>
<form method="post">
<input @默 .UserName />
<input @默 .Email />
<input @默 .PhoneNo />
<input hidden @默 .ProtectedId />
<button type="submit" >Submit</button>
<a asp-controller="Home" asp-action="List">Back</a>
</form>

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-06-06T13:48:51.017+00:00

    The error is very clear. You defined the View's model as EditMemberViewModel but passed the type MemberViewModel. Either fix the action to pass EditMemberViewModel to the view or change the View's model definition to MemberViewModel.

    Or perhaps you need to rethink the design?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.