加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

.net – 从控制器返回一个EditorTemplate作为部分视图

发布时间:2020-12-15 23:48:10 所属栏目:asp.Net 来源:网络整理
导读:我想从我的控制器返回一个EditorTemplate作为部分视图. 我目前在做: public ActionResult Create([Bind(Prefix="Create")]CreateViewModel model){ return PartialView("~/Views/Shared/EditorTemplates/Template.cshtml",model);} 问题是,在我这样做之后,C
我想从我的控制器返回一个EditorTemplate作为部分视图.

我目前在做:

public ActionResult Create([Bind(Prefix="Create")]CreateViewModel model)
{
    return PartialView("~/Views/Shared/EditorTemplates/Template.cshtml",model);
}

问题是,在我这样做之后,Create_前缀远离我的观点.有没有办法返回编辑器模板作为部分视图并保留前缀?

Index.cshtml
@model IndexViewModel

@using(Html.BeginForm("Create"))
{
    @Html.EditorFor(m => m.Create,"Template")

    <input type="submit" value="Save" />
}

我用AJAX电话提交此表单.当我最初调用EditorFor时,所有的字段都有一个Create_的前缀.但是,在提交表单并返回此PartialView后,该前缀将丢失.

解决方法

由于模板在主视图的上下文中没有被调用,所以它失去了上下文.在这种情况下,您可以定义前缀如下:
public ActionResult Create([Bind(Prefix="Create")]CreateViewModel model)
{
    ViewData.TemplateInfo.HtmlFieldPrefix = "Create";
    return PartialView("~/Views/Shared/EditorTemplates/Template.cshtml",model);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读