Home > database >  The model item passed into the ViewDataDictionary is of type but this ViewDataDictionary instance re
The model item passed into the ViewDataDictionary is of type but this ViewDataDictionary instance re

Time:11-15

When I Want to post my Form in Asp.net.Mvc I get this Problem How can i fix that

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'FormValidation.Models.Entity.Personel', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[FormValidation.Models.Entity.Personel]'.

Controller

[HttpGet]
        public IActionResult Index()
        {
           

           
            return View();
        }


        [HttpPost]
        public IActionResult Post(Personel personel)
        {

            return View(personel);
        }

form index

@model FormValidation.Models.Entity.Personel
<form method="post" asp-action="Post" asp-controller="Home">
    <div >
        <div >
            <div >
                <label for="Name"  placeholder="Enter Your Name">Name</label>
                <input type="text"  asp-for="Name" />

            </div>
            <br />
            <div >
                <label for="Surname"  placeholder="Enter Your Surname">Surname</label>
                <input type="text"  placeholder="Enter Your surname" asp-for="Surname" />
            </div>
            <br />
            <br />
            <div >
                <label for="City">City select</label>
                <select  asp-for="City">
                    <option></option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                </select>
            </div>
            <br />
            <br />
            <br />
            <div >
                <label for="Age"  placeholder="Enter Your Age">Age</label>
                <input type="number"  placeholder="Enter Your Age" asp-for="Age" />

            </div>
            <br />
            <div >
                <label for="Adress">Adres</label>
                <textarea  rows="3" asp-for="Adress">Adres</textarea>
            </div>
        </div>
    </div>
    <br />

    <input type="submit" name="submit" value="submit"  />
</form>

Post index

@model IEnumerable<FormValidation.Models.Entity.Personel>
   

@foreach (var item in Model)
{
    <p>@item.Name</p>
    <p>@item.Surname</p>
    <p>@item.Age</p>
    <p>@item.City</p>
    
}

CodePudding user response:

just as the error indicates,you passed the single personel model to render the view with return View(personel);

However,the View requires IEnumerable<Personel>

If you just want to show the details of the model you just created ,modify your view :

@model FormValidation.Models.Entity.Personel
.......
    <p>@Model.Name</p>
    <p>@Model.Surname</p>
    <p>@Model.Age</p>
    <p>@Model.City</p>
  • Related