0

Hi i'm trying to load a partial view in a modal on a view that requires a List<> model when i click the New Role button. Please what am i missing? I get the error

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Vidly.Models.AvailableRole]', but this dictionary requires a model item of type 'Vidly.ViewModel.RoleMenuViewModel'.

Model

public class RoleMenuViewModel
{
    public RoleMenuViewModel()
    {
        //Menus = new List<Menu>();

        MenuIds = new List<string>();
    }
    public List<string> MenuIds { get; set; }
    public MultiSelectList Menus { get; set; }
    public AvailableRole AvailableRoles { get; set; }
    public List<AvailableRole>  RoleMenu { get; set; }
    }

Partial View

@model Vidly.ViewModel.RoleMenuViewModel
@{
 ViewBag.Title = "Edit Role";
}
<legend>Create or Edit a Role</legend>

@using (Html.BeginForm("SaveRole", "Uam"))
{ 
    <div class="form-group">
        @Html.TextBoxFor(m => m.AvailableRoles.Name, new { @class = "form-control", PlaceHolder = "Role Name" })
        @Html.ValidationMessageFor(m=>m.AvailableRoles.Name)
    </div>

Main View

@model List<Vidly.Models.AvailableRole>

@{
ViewBag.Title = "CreateRole";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Available Roles</h2>

<div id="addRoleForm">
<button class="btn btn-success pull-right addRole"  type="button" data-
toggle="modal">New Role</button>
</div>

Controller

public ActionResult Index()
    {
        //get all roles in db
        var availableRoles = dbContext.AvailableRoles.OrderBy(a=>a.Name).ToList(); 

        return View("CreateRole", availableRoles);
    }

 public ActionResult CreateRole(int? id)
    {

        if (id == null)
        {

            var menuViewModel = new RoleMenuViewModel
            {
                 Menus = GetMultiselectItems()
            };

            return View("SaveRole", menuViewModel);
        }

JavaScript

<script type="text/javascript">
$(".addRole").on("click",
            function() {

                bootbox.dialog({
                    title: "Create new role",
                    message: "@Html.Partial("SaveRoles")"

                });
            });
  • `@Html.Partial()` does not call a server method (see _Passing the wrong model from a view to a partial view_ of the dupe) –  Jun 26 '17 at 21:39
  • I see, any ideas on how to achieve the result of loading the partial view to the modal? I think my trouble is that one view requires an enumerable list while the other doesn't so can't reference both in the same view – user8175382 Jun 26 '17 at 22:34
  • If your wanting to render the partial generated by `CreateRole()` method, then its `@Html.Action("CreateRole")` although its not clear why you have an `int? id` parameter when its never used. –  Jun 26 '17 at 22:48
  • Thanks that worked but it renders the partial view on page load not when I click on the button, please help. I have int? id because I attach two conditions to the controller. 1 for where the id is null and another where the id isn't so i figured i could use for both. – user8175382 Jun 26 '17 at 23:06
  • Yes of course, and so would your `@Html.Partial()` - both are razor code and are evaluated on the server before the view is sent to the browser. You need ajax if you want to call a server method to load content after the view has been sent to the client. –  Jun 26 '17 at 23:07
  • Please can you assist with the right ajax. Tried this but not working – user8175382 Jun 26 '17 at 23:41
  • Ask a new question and show your code and indicate what is not working –  Jun 26 '17 at 23:42

0 Answers0