简体   繁体   English

如何在主视图中渲染 PartialView

[英]how to Render a PartialView inside a main view

I'm working on an application, where I need to add a create View to enter data inside the Index View (where I'm showing all the data in a table from the database), to enter data from the same view.我正在开发一个应用程序,我需要添加一个创建视图以在索引视图中输入数据(我在其中显示数据库表中的所有数据),以从同一视图输入数据。 The Create View has rendered without any problem, but an error occurs on postback: error="child-actions-are-not-allowed-to-perform-redirect-actions" .创建视图已呈现没有任何问题,但在回发时出现错误: error="child-actions-are-not-allowed-to-perform-redirect-actions"

Controller:控制器:

// 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);
}

Here is Index.cshtml :这是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

Try using尝试使用

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

or

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

Adding in the correct path to the "_Create" view as per your Create method.根据您的 Create 方法添加到“_Create”视图的正确路径。 In turn the create method can be deleted.反过来可以删除 create 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM