简体   繁体   English

MVC在模态中创建局部视图

[英]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: 因此,我有一个名为_CreateClient的局部视图,该视图需要传递一个ClientEditViewModal,应从控制器传递它,如下所示:

    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: 但是在我的索引视图中,我需要将partial作为模态窗口调用,我这样做是这样的:

<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: 但是我的索引视图需要采用一个ClientModel的IEnumerable,当我尝试呈现索引视图时出现此错误:

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? 如何将视图传递给_CreateClient并将其保留在模式中? 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 好的,所以我想出了...我觉得有点荒谬,因为我需要做的就是给部分新的ViewModal ...是的

ANSWER: 回答:

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

You can pass the model as the second argument. 您可以将模型作为第二个参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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