【问题标题】:how to Render a PartialView inside a main view如何在主视图中渲染局部视图
【发布时间】:2016-07-13 10:07:10
【问题描述】:

我正在开发一个应用程序,我需要添加一个创建视图以在索引视图中输入数据(我在其中显示数据库表中的所有数据),以从同一视图输入数据. Create View 已经渲染没有任何问题,但是在回发时出现错误:error="child-actions-are-not-allowed-to-perform-redirect-actions"

控制器:

// GET: /Brands/
public async Task<ActionResult> Index()
{
    return View(await db.Brands.ToListAsync());
}

// GET: /Brands/Create
public ActionResult Create()
{
    return PartialView("_Create", new BootstrapModalTest.Models.Brand());
}

// POST: /Brands/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include="BrandId,BrandName,BrandCode")] Brand brand)
{
    if (ModelState.IsValid)
    {
        db.Brands.Add(brand);
        await db.SaveChangesAsync();
        return PartialView("_Create", brand);
    }

    return PartialView("_Create", brand);
}

这里是Index.cshtml

div class="panel panel-primary">
<div class="panel-heading" style=" padding:2px 7px 0px 6px;"><h4>Frames</h4></div>

<div class="panel-body " style="padding:4px;">
    @{Html.RenderAction("Create", "Brands");}
</div>

<table class="table table-bordered>
    // some data

【问题讨论】:

  • 你有什么问题?
  • m 收到错误“child-actions-are-not-allowed-to-perform-redirect-actions”
  • 在问题中解释(不是在 cmets 中!)
  • 回发时出现此错误

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

尝试使用

@Html.Partial("../Controller/View", model)

@Html.Partial("~/Views/Controller/View.cshtml", model)

根据您的 Create 方法添加到“_Create”视图的正确路径。 反过来可以删除create方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 2016-02-28
    相关资源
    最近更新 更多