1
\$\begingroup\$

My Model

public class AccountListFilterModel
{
    public int page { get; set; }
    public int pagesize { get; set; }
    public string sortOrder { get; set; }
    public string packageTypeFilter { get; set; }
    public string usernameFilter { get; set; }
    public string accountStatusFilter { get; set; }
    public string nameSurnameFilter { get; set; }
    public DateTime startDate { get; set; }
    public DateTime endDate { get; set; }
    public string dateFilter { get; set; }
    public int screenCountFilter { get; set; }

}

My Controller

 public ViewResult Index(AccountListFilterModel accountListFilter){}

My View

@{
var currentPageNumber = Request["page"];
var currentPageSize = Request["pagesize"];
var currentSortOrder = Request["sortOrder"];
var currentUsernameFilter = Request["usernameFilter"];
var currentAccountStatusFilter = Request["accountStatusFilter"];
var currentPackageTypeFilter = Request["packageTypeFilter"];
var currentnameSurnameFilter = Request["nameSurnameFilter"];
var currentDateFilter = Request["dateFilter"];
var currentScreenCountFilter = Request["screenCountFilter"];
var currentScreenCountSortFilter = Request["screenCountSortFilter"];}

and

                <th class="">Account Name
                    <a style="@(currentSortOrder == "UserName desc" ? "" : "display:none;")" href="@Url.Action("Index", "Account", new { pagesize = currentPageSize, sortOrder = "UserName asc", usernameFilter = @currentUsernameFilter, accountStatusFilter = @currentAccountStatusFilter, packageTypeFilter = @currentPackageTypeFilter, dateFilter = @currentDateFilter, screenCountSortFilter = @currentScreenCountSortFilter, screenCountFilter = @currentScreenCountFilter })">
                        <i class="icon-sort-up"></i></a>
                    <a style="@(currentSortOrder == "UserName asc" ? "" : "display:none;")" href="@Url.Action("Index", "Account", new { pagesize = currentPageSize, sortOrder = "UserName desc", usernameFilter = @currentUsernameFilter, accountStatusFilter = @currentAccountStatusFilter, packageTypeFilter = @currentPackageTypeFilter, dateFilter = @currentDateFilter, screenCountSortFilter = @currentScreenCountSortFilter, screenCountFilter = @currentScreenCountFilter,approvedFilter=@currentApprovedFilter })">
                        <i class="icon-sort-down"></i></a>
                    <a style="@(string.IsNullOrEmpty(currentSortOrder) || !currentSortOrder.StartsWith("UserName") ? "" : "display:none;")" href="@Url.Action("Index", "Account", new { pagesize = currentPageSize, sortOrder = "UserName asc", usernameFilter = @currentUsernameFilter, accountStatusFilter = @currentAccountStatusFilter, packageTypeFilter = @currentPackageTypeFilter,dateFilter = @currentDateFilter, screenCountSortFilter = @currentScreenCountSortFilter, screenCountFilter = @currentScreenCountFilter })">
                        <i class="icon-sort"></i></a>
                </th>

How can I shorten my code at View ?

\$\endgroup\$
  • 2
    \$\begingroup\$ Seems like you have a lot of logic in your markup, using MVC you're supposed to split these (logic <-> view). Calculate the desired outcome in the controller and pass those values to the view. \$\endgroup\$ – Abbas Dec 10 '14 at 10:06
2
\$\begingroup\$

You should pass your AccountListFilterModel to the view, either via model (which is the best solution as it's less error-prone) or using ViewBag \ ViewData (which is an anti-pattern actually, but fits the quick-fix niche). Using the latter approach:

In your controller:

public ViewResult Index(AccountListFilterModel accountListFilter)
{
    ViewBag.Filter = accountListFilter;
}

In your view (you can delete the code at the top of the view, that uses Request to get parameters.

<a style="@(ViewBag.Filter.currentSortOrder == "UserName desc" ? "" : "display:none;")" ... />

It's a general rule of thumb that your view should not contain any logic at all, your controller should supply all the data that the view needs.

\$\endgroup\$
  • \$\begingroup\$ Thanks. But What about @Url.Action("Index", "Account", new { pagesize = currentPageSize, sortOrder = "UserName asc", usernameFilter = @currentUsernameFilter...}. This is the main problem. I change the value and send model. How is be my Url.Action? \$\endgroup\$ – Altuğ Gülsepet Dec 10 '14 at 11:54
  • \$\begingroup\$ If you change the values, then you have some sort of a form or what? \$\endgroup\$ – Vsevolod Goloviznin Dec 10 '14 at 11:55
  • \$\begingroup\$ I mean my code must be according to your answer: @Url.Action("Index","Account",new{ViewBag.Filter}). But at this action i change the one value of ViewBag.Filter . Namely, @Url.Action("Index","Account",new{ViewBag.Filter.sortOder="Username asc"}). But at this code i am sending a one parameter. I wanna send amended ViewBag.Filter Model. \$\endgroup\$ – Altuğ Gülsepet Dec 10 '14 at 12:02
  • \$\begingroup\$ You can just do @Url.Action("Index", "Account", new { accountListFilter = ViewBag.Filter}. If you want to change the value, jsut do ViewBag.Filter.yourvalue = "something" \$\endgroup\$ – Vsevolod Goloviznin Dec 10 '14 at 12:18
  • \$\begingroup\$ I couldn't tell but thanks enough for me :) \$\endgroup\$ – Altuğ Gülsepet Dec 10 '14 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.