ASP.NET MVC: page-dependent logic

There was a lot of discussion of ASP.NET MVC and Codebehind-files , mainly where it was noted that these Codebehind files are evil .

So my question is: how do you handle page logic ?

What we donโ€™t need is spaghetti code in the embedded code , and we donโ€™t want the page-specific code to be scattered around the helper classes or at the top of the HTML helper class.

Example:

<% for(int i = 0; i < companyList.Count; i++) { %>
    RenderCompanyNameWithRightCapsIfNotEmpty(company, i)
<% } %>

With accompanying code:

private string RenderCompanyNameWithRightCapsIfNotEmpty(string company, index)
{
    if (index == 0) {
        return string.Format("<div class=\"first\">{0}</div>", company);
    }
    // Add more conditional code here
    // - page specific HTML, like render a certain icon
    string divClass = (index % 2 == 0) ? "normal" : "alternate";
    return string.Format("<div class=\"{1}\">{0}</div>", company, divClass);
}

This will only be used on one page and is likely to be changed.

Update : a couple comes up, I was thinking about where these are:

1) Inline codebehind - , .

<script runat="server">
    private string RenderCompanyHtml(string companyName) ...
<script>

2) , . View-logic .

public class SomeController : Controller 
{
    [NonAction] 
    private static string RenderCompanyHtml(string companyName) ...

    public ActionResult Index() ...
}
+3
3

controlaction, .

" " [NonAction], .

, () :

public class SomeController : Controller 
{
  #region Helper methods
  [NonAction] 
  private static string CompanyNameWithRightCapsIfNotEmpty(string company)
  {
    if (string.IsNullOrEmpty(company)) {
        return company;
    }
    return UpperCaseSpecificWords(company);
  }

  #endregion

  public ActionResult Companies()
  {
    var companies = GetCompanies();
    var companyNames = companies.Select(c =>  CompanyNameWithRightCapsIfNotEmpty(c.Name));
    ViewData["companyNames"] = companyNames;
    return view();
  }
}
+3

- , , , .

, , , . RenderCompanyNameWithRightCapsIfNotEmpty , , . , (, IEnumerable ).

+2

Html Helpers.

static:

    public static string Label(this HtmlHelper helper, string target, string text)
    {
        return String.Format("<label for='{0}'>{1}</label>", target, text);
    }

.. :

<span><% =Html.Label("FinishDateTime.LocalDatetime", "Finish Time:")%><br />

, RenderCompanyName(string[] companies), , html - , .

: - .. . , Html.

: , :

IList<> html <ul>...</ul>. , , css , html/content . - :

    public static string UnorderedList<TItem>(this HtmlHelper helper,
        IList<TItem> items, Func<TItem, string> renderItemHtml,
        string ulID, string ulClass, string liClass)
    {
        StringBuilder sb = new StringBuilder();

        // header
        if (!ulID.IsNullOrTrimEmpty()) sb.AppendFormat("<ul id='{0}'", helper.Encode(ulID.Trim()));
        else sb.AppendFormat("<ul");
        if (!ulClass.IsNullOrTrimEmpty()) sb.AppendFormat(" class='{0}'>", helper.Encode(ulClass.Trim()));
        else sb.AppendFormat(">");

        // items
        foreach (TItem i in items)
        {
            if (!liClass.IsNullOrTrimEmpty())
                sb.AppendFormat("<li class='{0}'>{1}</li>", helper.Encode(liClass.Trim()),
                    renderItemHtml(i));
            else
                sb.AppendFormat("<li>{0}</li>", renderItemHtml(i));
        }

        // footer
        sb.AppendFormat("</ul>");

        return sb.ToString();
    }

.. using easily. here is a simple example to display a list of tags:

    <div id="tags">
        <h2>Tags</h2>
        <%=Html.UnorderedList<Tag>(Model.Tags.Tags,tag=>
            {
                return tag.Name;
            },null,null,null) %>
    </div>

.. you can see in my usage example that I decided not to specify any css or id attribute, and I just return the element name Tagthrough the use of an anonymous delegate. Anonymous delegates are easy to use. In your case, there might be something like this:

    <div id="tags">
        <h2>Tags</h2>
        <%=Html.UnorderedList<string>(ViewData["companies"],company=>
            {
                if (someCondition) return company.ToUpper();
                else return company;
            },null,null,null) %>
    </div>

.. ViewData["companies"]is IList<string>for simplicity.

+1
source

Source: https://habr.com/ru/post/1709503/


All Articles