asp.net-mvc – 如何将模型传递给部分视图
发布时间:2020-12-15 23:44:15 所属栏目:asp.Net 来源:网络整理
导读:我有两个视图模型: public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child{ get; set; } }public class ChildViewModel { public ChildId { get; set; } ..... } 控制器: public ActionResult Index() { .... some c
我有两个视图模型:
public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child{ get; set; } } public class ChildViewModel { public ChildId { get; set; } ..... } 控制器: public ActionResult Index() { .... <some code> return View("NewIndex",ParentViewModel); } [HttpPost] public ActionResult PartialAction(ChildViewModel childView) { return RedirectToAction("Index"); } 意见: @model ParentViewModel .... @Html.Partial("_Partial",Model.Child) 和_Partial @model ChildViewModel ... do some stuff with child model 当我尝试打开索引页面时,我有一个错误: 传入字典的模型项目的类型为“ParentViewModel”,但此字典需要一个类型为“ChildViewModel”的模型项目. 为什么它尝试通过ParentViewModel而不是ChildViewModel.我做错了什么? 解决方法
我和OP有同样的问题.从其中一个意见中,我意识到第二个参数不应该为null,即从
@model ParentViewModel @Html.Partial("_Partial",Model.Child) 如果Model.Child为空,则传递Model而不是Model.Child.如果第二个参数为空,那么您将首先检查代码,然后将初始化的Child作为第二个参数传递.这样的东西 @Html.Partial("_Partial",new Child()) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 浅谈ASP.NET配置文件加密
- asp.net-mvc – ASP.NET MVC – Html.Textbox()throws“对象
- asp.net-mvc – 在“剃刀”视图中默认一个部分
- asp.net – 获取当前用户的GUID?
- asp.net – 如何单元测试使用HostingEnvironment.MapPath的
- asp.net – 如何防止PDF文档下载到用户PC?
- js_jquery_创建cookie有效期问题_时区问题
- asp.net – JObject.Parse与JsonConvert.DeserializeObject
- ASP.NET Url重写
- asp.net CustomValidator从不触发OnServerValidate
推荐文章
站长推荐
- asp.net – 403禁止错误
- asp.net-mvc-3 – @ Html.DropDownList width
- asp.net – linq foreach表现
- asp.net – 无法从App.Config中检索密钥
- 从ASP.NET Core 3.0 preview 特性,了解CLR的Gar
- ASP.NET MVC:如何将多个对象传输到View方法?
- asp.net – 在Web.Config的Location Path元素中指
- asp.net-mvc – 使用城堡温莎的实体框架
- asp.net – 将* .domain.com和domain.com重定向到
- asp.net-mvc-4 – 没有为此对象定义的无参数构造
热点阅读