I am trying to make the search functionality work only for the edit page. Even though my homecontroller for the main page does not have any link for the viewbag, it’s shown on every page, how can I avoid it for the main page only?
My Home Controller for the main page:
public IActionResult Index()
{
return View();
}
Other Controllers for the Blog Page:
public IActionResult Index(string searchString, string Category)
{
var blogs = Repository.Blogs;
if (!string.IsNullOrEmpty(searchString))
{
ViewBag.searchString = searchString;
blogs = blogs.Where(b => b.PostName.ToLower().Contains(searchString)).ToList();
}
if (!string.IsNullOrEmpty(Category) && Category != "0")
{
ViewBag.searchString = searchString;
blogs = blogs.Where(b => b.CategoryId == int.Parse(Category)).ToList();
}
ViewBag.Categories = new SelectList(Repository.Categories, "CategoryId", "Name");
var model = new BlogViewModel
{
Blogs = blogs,
Categories = Repository.Categories,
SelectedCategory = Category
};
return View(model);
}
public IActionResult Create()
{
ViewBag.Categories = new SelectList(Repository.Categories, "CategoryId", "Name");
return View();
}
[HttpPost]
public async Task<IActionResult> Create(Blog model, IFormFile imageFile)
{
var allowedExtension = new[] { ".jpg", ".jpeg", ".png" };
var extension = Path.GetExtension(imageFile.FileName);
var randomFile = string.Format($"{Guid.NewGuid().ToString()} {extension}");
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", randomFile);
if (imageFile != null)
{
if (!allowedExtension.Contains(extension))
{
ModelState.AddModelError("", "Please select a picture.");
}
}
if (ModelState.IsValid)
{
using (var stream = new FileStream(path, FileMode.Create))
{
await imageFile.CopyToAsync(stream);
}
model.Image = randomFile;
Repository.CreateBlog(model);
return RedirectToAction("Index");
}
ViewBag.Categories = new SelectList(Repository.Categories, "CategoryId", "Name");
return View();
}
public IActionResult Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var entity = Repository.Blogs.FirstOrDefault(b => b.Id == id);
if (entity == null)
{
return NotFound();
}
ViewBag.Categories = new SelectList(Repository.Categories, "CategoryId", "Name");
return View(entity);
}
[HttpPost]
public async Task<IActionResult> Edit(int id, Blog model, IFormFile? imageFile)
{
if (id != model.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
if (imageFile != null)
{
var extension = Path.GetExtension(imageFile.FileName);
var randomFile = string.Format($"{Guid.NewGuid().ToString()} {extension}");
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", randomFile);
using (var stream = new FileStream(path, FileMode.Create))
{
await imageFile.CopyToAsync(stream);
}
model.Image = randomFile;
}
Repository.EditBlog(model);
return RedirectToAction("Index");
}
ViewBag.Categories = new SelectList(Repository.Categories, "CategoryId", "Name");
return View(model);
}
public IActionResult Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var blog = Repository.Blogs.FirstOrDefault(b => b.Id == id);
if (blog == null)
{
return NotFound();
}
ViewBag.Categories = new SelectList(Repository.Categories, "CategoryId", "Name");
return View(blog);
}
[HttpPost]
public async Task<IActionResult> Delete([FromForm] int id)
{
var blog = Repository.Blogs.FirstOrDefault(b => b.Id == id);
if (blog == null)
{
return NotFound();
}
Repository.Blogs.Remove(blog);
return RedirectToAction("Index");
}
My Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<a href="Home/Article1"> <img src="~/img/bannernew.png" class="container-fluid" /> </a>
<br /><br />
<body style="background-color:whitesmoke">
</body>
<section class="call-to-action">
<div class="container">
<div class="banner">
<span><i>Digital Marketing</i></span>
<h3>Ultimate Guide For Digital Marketing-FREE!</h3>
<div class="col-lg-4">
<div class="main-button">
<div class="row">
<div class="col-lg-12">
<div class="main-content">
<div class="row">
<div class="col-lg-8">
</div>
<div class="col-lg-6">
<p><a style="text-align:center;" href="https://www.digitalmarketer.com/digital-marketing/assets/pdf/ultimate-guide-to-digital-marketing.pdf" class="btn btn-secondary">Download Now!</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br /><br />
</section>
<section class="blog-posts">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="all-blog-posts">
<div class="row">
<div class="col-lg-12">
<div class="blog-post">
<div class="blog-thumb">
<img src="~/img/1.jpg" alt="">
</div>
<div class="down-content">
<p style="color:blue"><strong><i>Technology</i></strong></p>
<a href="/Home/Article1" class="typo"><h3>An Actionable 10-Step Guide to Launch A Tech Startup</a></h3>
<ul class="post-info">
<li class="admin1">Busra Nur</a></li>
<li class="may1">May 31, 2023</a></li>
<li class="comments1">12 Comments</a></li>
</ul>
<p style="color:darkblue;">Starting a tech business means understanding and dealing with finance, legal, sales, and marketing issues. As you are interested in starting a tech business, you have taken the leap of faith to become a doer.<a style="color:darkblue;"href="/Home/Article1" class="seemore1">See More</a></p>
<div class="post-options">
<div class="row">
<div class="col-6">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12">
<div class="blog-post">
<div class="blog-thumb">
<img src="~/img/2.jpg" alt="">
</div>
<div class="down-content">
<p style="color:blue"><strong><i>Entrepreneurship</i></strong></p>
<a href="/Home/Article2" class="typo1"><h3>Top Challenges Faced by Non-tech Founders</a></h3>
<ul class="post-info">
<li class="admin2">Merve Cicek</a></li>
<li class="Jan2">Jan 24, 2024</a></li>
<li class="comments2">36 Comments</a></li>
</ul>
<p style="color:darkblue;">If you have a great idea for an app or software, starting a tech business might seem like the obvious choice.But it’s not as simple as it seems. The world of technology can be intimidating for non-tech founders who may not understand the ins and outs of these products.<a style="color:darkblue;"href="/Home/Article2" class="seemore2">See More</a></p>
<div class="post-options">
<div class="row">
<div class="col-6">
</div>
<div class="col-6">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12">
<div class="blog-post">
<div class="blog-thumb">
<img src="~/img/3.jpg" alt="">
</div>
<div class="down-content">
<p style="color:blue"><strong><i>Technology</i></strong></p>
<a href="/Home/Article3" class="typo2"><h3>How to Redesign An App for Scaling: The Complete Guide</a></h3>
<li class="admin3">Hande Gul</a></li>
<li class="Feb3">Feb 14, 2023</a></li>
<li class="Comments3">48 Comments</a></li>
</ul>
<p style="color:darkblue;">Read on to understand how to revamp your app and prepare it for a scaling process without affecting your current performance or bringing your business to a standstill.<a style="color:darkblue;"href="/Home/Article3" class="seemore3">See More</a></p>
<div class="post-options">
<div class="row">
<div class="col-6">
</div>
<div class="col-6">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-12">
<div class="main-button">
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="sidebar">
<div class="row">
<div class="col-lg-12">
<div class="sidebar-item search">
</div>
</div>
<div class="col-lg-12">
<div class="sidebar-item recent-posts">
<div class="sidebar-heading1">
<h2>Recent Posts</h2>
</div>
<div class="content1">
<ul>
<li>
<a href="/Home/Article1" class="content1">
<h5 style="color:darkslateblue;">An Actionable 10-Step Guide to Launch A Tech Startup</h5>
<span style="color:darkslateblue;">May 31,2023</span>
</a>
</li>
<li>
<a href="/Home/Article2" class="content2">
<h5 style="color:darkslateblue;">Top Challenges Faced by Non-tech Founders</h5>
<span style="color:darkslateblue;">Jan 24, 2024</span>
</a>
</li>
<li>
<a href="/Home/Article3" class="content3">
<h5 style="color:darkslateblue;">How to Redesign An App for Scaling: The Complete Guide</h5>
<span style="color:darkslateblue;">Feb 14, 2023</span>
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="col-lg-12">
<div class="sidebar-item categories">
<div class="col-lg-12">
<div class="sidebar-item tags">
<div class="sidebar-heading2">
</div>
<div class="content">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
I didn’t put any link or reference to viewbag in my views. Only they’re given in the other pages.
I tried to add some functionality to hide the viewbag in the main page for the views, but it didn’t work. I expect to work it properly if there is a solution.