locked
more model in a view RRS feed

  • Question

  • User-1471881183 posted

    hello all,

    i have some models like below, i would like use them in one view and they dont have any relationships. so, i used below code but, i keeps on getting issue as like below

    An unhandled exception occurred while processing the request.

    InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'CoreWebApplication.Models.RealEx', but this ViewDataDictionary instance requires a model item of type 'CoreWebApplication.Models.ModelCollection'.
    Models

    public class RealEx
    {
    [Required]
    public string Key { set; get; }
    public string number { set; get; }

    ....

    }

    public class Employee
    {
    public int EmployeeID { get; set; }
    public string Sid { get; set; }

    }

    public class Tokenization
    {
    public int EmployeeID { get; set; }
    public string Sid { get; set; }

    }

      public class ModelCollection
        {
            public Tokenization Token { get; set; }
            public Employee Emp { get; set; }
            public RealEx Real { get; set; }
     
            public ModelCollection()
            {
                Token = new Tokenization();
                Emp = new Employee();
                Real = new RealEx();
            }
        }
    Controller
    public ActionResult TokenModelCollection(ModelCollection mc)
    {
    string strClient = mc.Real.Key;
    return View(mc.Real);
    }
    Model
    @model WebApplication.Models.ModelCollection
     
    @{
        ViewData["Title"] = "ModelCollection";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
     
    <h1>ModelCollection</h1>
     
    <h4>ModelCollection</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="TokenModelCollection">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    @*<input asp-for="Real.Key" class="form-control" />*@
                    @*@Html.TextBox(model => model.Real.Key)*@
                    <input type="text" asp-for="Real.Key" />
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    experts please help me to resolve this
    thanks in advance.
    Thursday, September 26, 2019 3:19 PM

Answers

  • User-1471881183 posted

    sorry i resolved this issue

    return View(mc.Real); its the error then i changed it into return View(mc);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 26, 2019 3:34 PM