Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have A problem in Code-First in project asp.net mvc when i Click Create Action or Click Edit Action the Error Message Is
"InvalidOperationException: The following sections have been defined but have not been rendered by the page at '/Views/Shared/_Layout.cshtml': 'Scripts'. To ignore an unrendered section call IgnoreSection("sectionName")."
but all action working well and my renderbody(); is written in my layout

What I have tried:

my Code
C#
public class NewsController : Controller
{
    private readonly NewsContext _context;
    
    public NewsController(NewsContext context)
    {
        _context = context;
    }
    
    // GET: News
    public async Task<iactionresult> Index()
    {
        return View(await _context.News.ToListAsync());
    }
    
    // GET: News/Details/5
    public async Task<iactionresult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }
    
        var news = await _context.News
            .FirstOrDefaultAsync(m => m.NewsId == id);
        if (news == null)
        {
            return NotFound();
        }
    
        return View(news);
    }
    
    // GET: News/Create
    public IActionResult Create()
    {
        return View();
    }
    
    // POST: News/Create
    // To protect from overposting attacks, 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<iactionresult> Create([Bind("NewsId,title,DateTime,Image,Topic,CatgID")] News news)
    {
        if (ModelState.IsValid)
        {
            _context.Add(news);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(news);
    }
    
    // GET: News/Edit/5
    public async Task<iactionresult> Edit(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }
    
        var news = await _context.News.FindAsync(id);
        if (news == null)
        {
            return NotFound();
        }
        return View(news);
    }
    
    // POST: News/Edit/5
    // To protect from overposting attacks, 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<iactionresult> Edit(int id, [Bind("NewsId,title,DateTime,Image,Topic,CatgID")] News news)
    {
        if (id != news.NewsId)
        {
            return NotFound();
        }
    
        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(news);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NewsExists(news.NewsId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(news);
    }

my View
HTML
@model NewsApp.Models.News

@{
    ViewData["Title"] = "Create";
     Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Create</h1>

<h4>News</h4>
<hr>
<div class="row">
    <div class="col-md-4">
        
            <div class="text-danger"></div>
            <div class="form-group">
                
                
                <span class="text-danger"></span>
            </div>
            <div class="form-group">
                
                
                <span class="text-danger"></span>
            </div>
            <div class="form-group">
                
                
                <span class="text-danger"></span>
            </div>
            <div class="form-group">
                
                
                <span class="text-danger"></span>
            </div>
            <div class="form-group">
                
                
                <span class="text-danger"></span>
            </div>
            <div class="form-group">
                
            </div>
        
    </div>
</div>

<div>
    <a>Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Posted
Updated 8-Sep-20 19:54pm
v2

1 solution

A similar discussion[^] - quoting from there for easy reference:

It means that you have defined a section in your master Layout.cshtml, but you have not included anything for that section in your View.

If your _Layout.cshtml has something like this:
HTML
@RenderSection("scripts")

Then all Views that use that Layout must include a @section with the same name (even if the contents of the section are empty):
Razor
@{
    ViewBag.Title = "Title";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    // Add something here
}

As an alternative, you can set required to false, then you won't be required to add the section in every View,
HTML
@RenderSection("scripts", required: false)

or also you can wrap the @RenderSection in an if block,
HTML
@if (IsSectionDefined("scripts"))
{
    RenderSection("scripts");
}
 
Share this answer
 
Comments
ahmedbelal 9-Sep-20 11:07am    
thanks my dear friend bu i made @Renderbody() ??
ahmedbelal 9-Sep-20 11:44am    
thanks Mr Sandeep everythings Ok
Sandeep Mewara 9-Sep-20 12:16pm    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900