将不同的模型传递给局部视图 [英] pass a different model to the partial view

查看:59
本文介绍了将不同的模型传递给局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将不同的模型从视图传递到局部视图.我对它们和两个不同的视图模型都有两个单独的控制器动作.但是当我从视图中调用局部视图时,它给了我错误

I am trying to pass a different model to the partial view from a view. I have two separate controller actions for both of them and two different view models. But when I call the partial view from within the view it gives me the error

传递到字典中的模型项的类型为'Application.ViewModels.Model1ViewModel',但是此字典需要类型为'Application.ViewModels.PartialViewModel'的模型项.

The model item passed into the dictionary is of type 'Application.ViewModels.Model1ViewModel', but this dictionary requires a model item of type 'Application.ViewModels.PartialViewModel'.

我这样称呼它:

 @Html.Partial("_CreateUniFunctionPartial")

视图中的模型调用为

@model Application.ViewModels.Model1ViewModel

,局部视图文件中的模型为

and model in partial view file is

@model Application.ViewModels.PartialViewModel

我不确定如何传递局部视图,因此不会出现此错误.

I am not sure how to pass the partial view so it doesnt give this error.

编辑

局部视图

@model Application.ViewModels.PartialViewModel



 @using (Html.BeginForm("partialview", "ApplicationDetail", FormMethod.Post)) 
  {


<div class="form-horizontal">
    <h4>UniFunctionViewModel</h4>
    <hr />
    @Html.ValidationSummary(true)





    <div class="form-group">
        @Html.LabelFor(model => model.detail, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.detail, new { @placeholder = "Further Information" })
            @Html.ValidationMessageFor(model => model.detail)
        </div>
    </div>


</div>

}

推荐答案

您使用的是正确的方法,但没有传入正确的参数

you are using the right method but not passing in the right arguments

您可能想这样尝试:

@Html.Partial("~/[path_to_root_only_if_exists]/_CreateUniFunctionPartial.cshtml", new Application.ViewModels.PartialViewModel())

如果您不传递模型,它将自动从其父模型中获取模型,在您的情况下为

if you do not pass in a model, it will automatically take the one from its parent, which in your case is

Application.ViewModels.Model1ViewModel

这篇关于将不同的模型传递给局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆