I have this checkbox and it works... but each time the form submits the checkbox gets unchecked. You wouldn't believe the time and frustration I've spent on forums and Google looking at all but the answer I need to this seemingly simple problem. Wasted morning.
Here's where the checkbox shows up in my view:
@using (Html.BeginForm())
{
<p> Search Criteria: @Html.TextBox("searchString") <br />
<input type="submit" value="Filter" /></p>
<p>Show only my posts: <input type="checkbox" name="authorFilter" onchange="this.form.submit();"/></p>
}
And here's where the controller handles this stuff:
public ActionResult Index(string searchString)
{
var posts = from p in db.BlogPosts
select p;
var authorFilterCheck = Request.Form["authorFilter"];
if (authorFilterCheck == "on")
{
string userID = User.Identity.GetUserId();
posts = posts.Where(i => i.AuthorID.Equals(userID));
}
if (!string.IsNullOrEmpty(searchString))
posts = posts.Where(
x =>
x.Body.Contains(searchString) ||
x.Title.Contains(searchString));
return View(posts);
}
So when it's checked, it adds a filter and the page refreshes. But when it refreshes the checkbox is unchecked (but the filter is still applied) so that you can never remove the filter (plus the empty checkbox missleads the user into thinking there's no filter)...
There's nothing about the checkbox in my model because presumably that would add its bool to all the entries in the table. Sorry, I'm new to all this.
Aucun commentaire:
Enregistrer un commentaire