0

Hi I'm using a HttpPost from a view to send some parameters to a controller, the db gets filtered according to those given parameters. BUT the parameters should be optional and if not given the controller just ignores them. But this seems to be impossible?

VIEW

@using (Html.BeginForm("Index", "Logbook")) {
 @Html.ValidationSummary(true)
       <div class="col-lg-3">
           <div class="panel panel-default">
               <div class="panel-heading">
                   <i class="fa fa-bell fa-fw"></i> Filter logbook
               </div>
               <div class="panel-body">
                   <div class="col-lg-5">
                        @Html.Label("Filter on room: ")
                        @Html.Label("Filter on date: ")
                   </div>
                   <div class="col-lg-6"> 
                        @Html.DropDownList("dropdownlist",Eindwerk.Controllers.RoomController.GetRooms(),new { @class = "btn btn-default-dropdown-toggle"})
                        @Html.TextBox("datepicker","", new { @class = "form-control" })
                   </div>
                <div>
                <button type="submit" class="btn btn-outline btn-primary btn-lg btn-block">Filter logbook</button>
                @Html.ActionLink("Remove Filter", "Index","FilterClear",new { @class = "btn btn-outline btn-success btn-lg btn-block" })
           </div>   

}

CONTROLLER

I thought it would be possible to just check if the parameter was null or not and change the function accordingly:

[HttpPost]
public ActionResult Index(string dropdownlist, DateTime datepicker)
{
    if(dropdownlist != null && datepicker != null)
    return View(db.Logbook.Where(p => p.Room == dropdownlist && p.Time.Day == datepicker.Month).OrderByDescending(a => a.Id).ToList());

    if(dropdownlist != null && datepicker == null)
    return View(db.Logbook.Where(p => p.Room == dropdownlist).OrderByDescending(a => a.Id).ToList());

    if(dropdownlist == null && datepicker != null)
    return View(db.Logbook.Where(p => p.Time.Day == datepicker.Month).OrderByDescending(a => a.Id).ToList());

    else
    return View(db.Logbook.OrderByDescending(p => p.Id).ToList());

}

But I get an instant error that the parameters should be given, is there a possibility to set these parameters to optional?

2
  • 2
    Declare the parameter as nullable DateTime? datepicker Commented Jun 11, 2014 at 11:45
  • Indeed, so easy but didn't think of it. Thanks! Can't accept as answer though it's a comment... Commented Jun 11, 2014 at 11:55

4 Answers 4

2

Declare the parameter as nullable

public ActionResult Index(string dropdownlist, DateTime? datepicker)
Sign up to request clarification or add additional context in comments.

Comments

0

declare nullable parameters

public ActionResult Index(string? dropdownlist, DateTime? datepicker)

2 Comments

As Stephen Muecke already said: the solution was to set DateTime? datepicker as nullable. On string this doesn't have to cause string can be null. thanks anyway!
You can't have a string? parameter. String is already "nullable" by default.
0

try this it can be work

public ActionResult Index(string dropdownlist=null, DateTime datepicker=null)

Comments

0

You can remedy this by making the parameter optional and assigning a default value of null:

public ActionResult Index(string dropdownlist, DateTime? datepicker = null){}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.