Skip to content
Loading
Error - The model item passed into the dictionary is of type  
  •       
  •     @using (Ajax.BeginForm("CreateEdit""Product"new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "frmProduct" }))  
  •     {  
  •         class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">  
  •             class="modal-dialog">  
  •                 class="modal-content">  
  •                     class="modal-header">  
  •                         "button" class="close" data-dismiss="modal">"true"class="sr-only">Close  
  •                         class="modal-title">Add New Product  
  •                       
  •                     class="modal-body" id="frmProduct">  
  •                         @Html.Partial("_CreateEdit",Model.Location) //Note here  
  •                       
  •                     class="modal-footer">  
  •                         "button" class="btn btn-secondary" data-dismiss="modal">Cancel  
  •                         "submit" class="btn btn-primary">Save Changes  
  •                       
  •                   
  •               
  •           
  •     }  
  •   
  •   
  • Note:- Also in the partial view, you don't need Layout reference so remove @{Layout.... line.} 
  • Hi Sachin
     
      Controller is - 
    public ActionResult Index()
    {
    return View(dbLocation.GetAllLocation().ToList());
    }
     
    On Click of Add or Edit button i want to call Partial View which is like below
     
    @model MyApplication.Models.Location
    @{
    Layout = "~/Views/Shared/_Layout.cshtml";
    }
    CreateEdit
    @using (Html.BeginForm("CreateEdit", "Location", FormMethod.Post, new { @id = "locationFormId", @class = "form-horizontal", role = "form" }))
    {
    @Html.AntiForgeryToken()

    @*

    Location


    *@
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)
    @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
    @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
    @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
    @Html.ActionLink("Cancel", "Index")
    }
    @*
    @Html.ActionLink("Back to List", "Index")
    *@
     
     
    Thanks 
  • Sachin Singh
    This means you are passing a simple object from the controller to view instead of a list.
    It should be
    1. public ActionResult Index()  
    2. {  
    3.  List mylist= new List();  
    4. // retrieve value from database and add to the list   
    5. //mylist.add();  
    6. return view(mylist);  
    7.   
    8. }