i am using asp.net mvc3 and i am going to implement advance search
this is my advance search form
@using (Html.BeginForm("AdvanceSearch","Coupon",FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.CouponName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.CouponName)
@Html.ValidationMessageFor(model => model.AdvanceSearch.CouponName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.Category)
</div>
<div class="editor-field">
@Html.DropDownList("categories", new SelectList(Model.AdvanceSearch.Category.OrderBy(c=>c.Name).Select(c => c.Name)), "--- Select Categories ---")
@Html.ValidationMessageFor(model => model.AdvanceSearch.Category)
</div>
<div class="editor-label">
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.CreateDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.CreateDate, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.AdvanceSearch.CreateDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.ExpiredDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.ExpiredDate, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.AdvanceSearch.ExpiredDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.PublishDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.PublishDate, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.AdvanceSearch.PublishDate)
</div>
@Html.LabelFor(model => model.AdvanceSearch.Company)
</div>
<div class="editor-field">
@Html.DropDownList("companies", new SelectList(Model.AdvanceSearch.Company.OrderBy(c=>c.Name).Select(c => c.Name)), "--- Select Companies ---")
@Html.ValidationMessageFor(model => model.AdvanceSearch.Company)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.Description)
@Html.ValidationMessageFor(model => model.AdvanceSearch.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.IsPublish)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.IsPublish)
@Html.ValidationMessageFor(model => model.AdvanceSearch.IsPublish)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdvanceSearch.Active)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdvanceSearch.Active)
@Html.ValidationMessageFor(model => model.AdvanceSearch.Active)
</div>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
}
now the problem is that i have following scenarios
- if only CouponName is given then all coupons contains specified name will be returned
- if any company is selected from company list then we will search all coupon from that company having specified name will be returned
- if any Category is selected from category list then we will search all coupon from that category having specified name will be returned
- if any date from all of threee date is been selected then we will filter coupon by that date
now i am confused that what is the best way to do that , should i implement if else conditions , switch or what ?
Not sure if there is a “best way”. It all depends on how you are applying the filter.
I use LINQ to apply filters and like to write my filter construction with a base expression, and then AND expressions for every provided filter selection. I use some extension methods to make this easier. Then you can build your filter expression in steps, and as it get more complex, you can implement steps accordingly. Each basic step would have a if check to see if a particular part was specified, and if it was append it to the base expression.