简体   繁体   中英

MVC Create A Partial View In A Modal

So I have a partial view called _CreateClient that needs to be passed a ClientEditViewModal that should get passed from the controller like so:

    public ActionResult Create()
    {
        ClientEditViewModel viewModel = new ClientEditViewModel();
        return PartialView("_CreateClient", viewModel);
    }

But in my Index view I need to call the partial as a modal window and I am doing it like this:

<div class="mfp-hide">   
    @Html.Partial("_CreateClient")
</div>

However my Index view needs to take an IEnumerable of ClientModel and I am getting this error when I try to render the Index view:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Project.Models.ClientModel]', but this dictionary requires a model item of type 'Project.ViewModels.ClientEditViewModel'.

How do I get the view passed to the _CreateClient and keep it in the modal? Or is there a better way to go about doing this?

Thanks!

EDIT:

Index Action:

public ActionResult Index()
{
    IEnumerable<ClientModel> clients = clientService.GetAll();
    return View(clients);
}

Index View:

@model IEnumerable<TechUCRM.Models.ClientModel>
@{
    Layout = "~/Views/Shared/_UserLayout.cshtml";
    ViewBag.Title = "Index";
}

@section alert {
    @if (!String.IsNullOrEmpty(ViewBag.alert))
    {
        <div class="alert alert-@ViewBag.alertType" role="alert">
            @ViewBag.alert
        </div>
    }
}
<div class="row">
    <div class="col-sm-6 text-left">
        <h2>Clients</h2>
    </div>


    <div class="col-sm-6 text-right">
        <label for="#add-client-btn">New Client</label>
        <a href="@Url.Action("Create")" id="add-client-btn" class="btn btn-default btn-sm"><i class="fa fa-plus fa-fw"></i></a>

    </div>


    <div class="mfp-hide">  
        @Html.Partial("_CreateClient") <!--Need to be passed ClientCreateViewModel-->
    </div>

</div>


<div class="row">
    @foreach(var item in Model)
    {
        <div class="col-md-3 col-xs-12" style="margin-top:20px;">
            <a href="" class="btn btn-primary btn-lg">@item.Name</a>
        </div>
    }
</div>

EDIT:

Ok so I figured it out... I feel a little ridiculous because all I needed to do was to give the partial a new ViewModal... yep

ANSWER:

@Html.Partial("_CreateClient", new ClientCreateViewModal()) 
@model ClientEditViewModel
@Html.Partial("_CreateClient", Model);

You can pass the model as the second argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM