【问题标题】:making my controller code cleaner and less repetitive使我的控制器代码更简洁,重复更少
【发布时间】:2016-04-27 22:41:58
【问题描述】:

我将在下面发布我的视图和控制器。

我想知道是否有更好的方法来做到这一点,所以我不需要重复 相同的代码 4 次,因为我的控制器和更好的实践是相当多的将是使其更可重用,而不是重复和更改微小的部分。

它的工作原理如下:用户从FirstNameEmailPostCode 中选择一个选项,然后可以选择一个时间范围,如下所示。为每个人做这个将是一个简单的复制/粘贴和更改,但如果可能的话,我想使用更少的代码。

这是我的控制器声明:

//if a user choose the radio button option as FirstName  
if (option == "FirstName")
{ 
    if (date == "day")
    {
        DateTime today = DateTime.Now.AddDays(-1);
        return View(db.Orders.Where(x => x.FirstName.StartsWith(search) && x.OrderDate >= today || search == null).ToList());
    }
    if (date == "week")
    {
        DateTime today = DateTime.Now.AddDays(-7);
        return View(db.Orders.Where(x => x.FirstName.StartsWith(search) && x.OrderDate >= today || search == null).ToList());
    }
    if (date == "month")
    {
        DateTime today = DateTime.Now.AddMonths(-1);
        return View(db.Orders.Where(x => x.FirstName.StartsWith(search) && x.OrderDate >= today || search == null).ToList());
    }
    if (date == "year")
    {
        DateTime today = DateTime.Now.AddYears(-1);
        return View(db.Orders.Where(x => x.FirstName.StartsWith(search) && x.OrderDate >= today || search == null).ToList());
    }
    else
    { return View(db.Orders.Where(x => x.FirstName.StartsWith(search) || search == null).ToList());
}

我的看法;

@using (Html.BeginForm("Index", "EditOrders", FormMethod.Get))
{
    //the following are search options

    <b> Search for: </b>@Html.RadioButton("option", "FirstName") <text>First Name</text> @Html.RadioButton("option", "PostalCode") <text> Post-Code </text>
    @Html.RadioButton("option", "Username")<text>Email-Address</text> @Html.TextBox("search")
    <text>Orders Made </text><select id="date" name="date">
        <option value="none">All Orders</option>
        <option value="day">In the last day</option>
        <option value="week">In the last week</option>
        <option value="month">In the last month</option>
        <option value="year">In the last year</option>
    </select>
    <input type="submit" name="submit" value="Search" />
}

【问题讨论】:

  • 这适用于工厂模式的实现;您的控制器看起来很简单,只有两行代码,但这些条件将出现在您的 Factory 类中,该类返回必要的对象
  • 另一种选择是将您的值更改为实际天数;例如:代替“day”,将其更改为“1”;而不是“周”,将其更改为“7”;使用该值减去控制器中的天数
  • 使用 IQueryable 并分别添加每个选项(名字、电子邮件、邮政编码、日期)。更多信息:stackoverflow.com/questions/22122618/…

标签: c# asp.net-mvc entity-framework razor


【解决方案1】:

如何更改表单中的实际值

    @using (Html.BeginForm("Index", "EditOrders", FormMethod.Get))
{
    //the following are search options
<b> Search for: </b>@Html.RadioButton("option", "FirstName") <text>First Name</text> @Html.RadioButton("option", "PostalCode") <text> Post-Code </text>
@Html.RadioButton("option", "Username")<text>Email-Address</text> @Html.TextBox("search")
<text>Orders Made </text><select id="date" name="date">
    <option value="0">All Orders</option>
    <option value="1">In the last day</option>
    <option value="7">In the last week</option>
    <option value="30">In the last month</option>
    <option value="365">In the last year</option>
    </select>
    <input type="submit" name="submit" value="Search" />
}

您的控制器代码

if (option == "FirstName")
{ 
    var days = Convert.ToInt32(date) * -1;

    if(days > 0)
    {
        DateTime today = DateTime.Now.AddDays(days);
        return View(db.Orders.Where(x => x.FirstName.StartsWith(search) && x.OrderDate >= today || search == null).ToList());
    }
    else
    {
         return View(db.Orders.Where(x => x.FirstName.StartsWith(search) || search == null).ToList());
    }
}

【讨论】:

  • 365 会中断闰年,30 会中断数月(有些是 31,有些是 30,2 月可以有 28 或 29)。另外,你的 else 坏了(search) &amp;&amp; || search
  • @user1666620 - 这是真的;这只有在他不关心这一点时才有效;
  • 我现在正在测试这个,如果它有效,这将是我的首选方法!不用担心闰年等。稍后会回复您,谢谢@techspider
  • @techspider 非常感谢!这完美地工作并大大清理了我的代码!
  • 这种方法对客户端的操作是开放的,如果这很重要的话。例如,我可以将日期值更改为 3650 并发布表单,我会看到过去 10 年的订单。
【解决方案2】:

这是视图和控制器之间的界限,哈哈。 我投票清除代码控制器代码而不是更改视图。 你可以使用factory 模式,如果你有时间,也可以看看干净的代码概念。在 MVA 上有一些很好的教训。 再次,是个人的。

最好的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多