ASP.NET MVC LIST和创建相同的看法 [英] ASP.NET MVC LIST and Create in same view

查看:107
本文介绍了ASP.NET MVC LIST和创建相同的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我有一个观点,多数民众赞成是强类型作为MyObject的集合

Ok I have a View thats is strongly typed as a collection of MyObject

@model IEnumerable<ViewModels.MyObject>

通过收集我环以创建对象列表

I loop through the collection to create a list of the objects.

在同一页上,我需要创建表单添加一个新的MyObject的。

On the same page I need to create form to add a new MyObject.

有没有办法使用HTML辅助沿着王氏拉姆达前pressions创建从我的模型强类型?元素的方式

Is there a way to use Html helpers along wih lambda expressions to create elements strongly typed from my model?

或将是最好的加载这个页面的形式部分作为类型为MyObject的(不是集合)?另一个局部视图

or would it be best to load the Form portion of this page as another partial view typed as MyObject(not a collection)?

推荐答案

您可以创建一个包含两个视图模型视图模型属性:

You can create view model with two view models as properties:

class ListCreateViewModel {
    ListViewModel ListViewModel { get; set; }
    CreateViewModel CreateViewModel { get; set; }
}

然后你可以使用

Html.RenderPartial("Create", Model.CreateViewModel);

您的部分将有 CreateViewModel 为模型。

您也可以完全独立的视图和模型,并呼吁:

You can also make totally separate view and model and call:

Html.RenderAction("Create", "Controller");

的RenderAction 表现为新的要求,并认为在不同的上下文中呈现。

RenderAction behaves as new request and view is rendered in different context.

这篇关于ASP.NET MVC LIST和创建相同的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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