单击后,在不同的VIEW中输入3个日期并显示顺序

时间:2012-12-07 21:00:29

标签: c# asp.net asp.net-mvc-3 html-parsing

嘿伙计们,所以我试图把一个项目放在一起,以增加我对MVC3的了解,但我已经碰壁了......

因此,在我的索引'view'中的HolidaysController中,我创建了一个超链接,将用户导航到'create3'ActionResult

    @ Html.ActionLink(“选择3个日期”,“创建3”)

在我的create3页面中,我希望用户在文本框中输入3个日期,当他们点击“创建”时,用户将返回到之前的HolidaysController / Index页面 其中日期将按升序日期的顺序显示

.... ATM我有这个工作,直到用户输入3个日期并点击'创建'...但是我只知道如何显示一个显示订单的消息框,它正在工作我只是需要帮助得到命令从索引页面显示。

请参阅我的代码:

HolidayController / Index的代码:

 //submit will go to post
        [HttpPost]
        public ViewResult Index(int HolidayDate)
        {
            var holidays = db.Holidays.Include("Person");

            HolidayList model = new HolidayList();

            model.currentPersonID = HolidayDate;
            model.PList4DD = db.People.ToList();           
            model.Categories = holidays.Select(x => new SelectListItem
                                            {
                                                Value = x.Id.ToString(),
                                                Text = x.Person.Name
                                            }
                                          );


            int data = HolidayDate;

            model.HList4DD = db.Holidays.Where(h => h.PersonId == HolidayDate).ToList();      

            return View(model);

        }

        [HttpGet]
        public ViewResult Index(string sortOrder, int? currentPersonID)
        {
            var holidays = db.Holidays.Include("Person");

            HolidayList model = new HolidayList();

            //not null
            if (currentPersonID.HasValue)
            {
                model.currentPersonID = currentPersonID.Value;

            }
            else
            {
                model.currentPersonID = 0;
            }

            model.PList4DD = db.People.ToList();

            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "date" : "";
            var dates = from d in db.Holidays
                        where d.PersonId == currentPersonID.Value
                        select d;

            switch (sortOrder)
            {
                case "date":
                    dates = dates.OrderBy(p => p.HolidayDate);
                    break;
            }

            model.HList4DD = dates.ToList();

            return View(model);
        }

//View for Index

@*@model IEnumerable<HolidayBookingApp.Models.Holiday>*@
@model HolidayBookingApp.Models.HolidayList
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<p>
@Html.ActionLink("Select 3 Dates", "Create3")
</p>

<table>
    <tr>
        <th>
            PersonId
        </th>
        <th>
            @*HolidayDate*@
            @Html.ActionLink("HolidayDate", "Index", new { sortOrder = ViewBag.NameSortParm, currentPersonID = Model.currentPersonID })
        </th>
        <th></th>
    </tr>
    <tr>  
    </tr>

@foreach (var item in Model.HList4DD)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PersonId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HolidayDate)

        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>}

    <tr>
      <div class="editor-label">
         @*   @Html.LabelFor(model => model.PList4DD, "Person")*@
        </div>

        <div class="editor-field">           
             &lt;form action ="/Holidays/Index" id="some" method="post"> 

  @Html.DropDownListFor(model => model.HList4DD.First().HolidayDate, new SelectList(Model.PList4DD, "Id", "Name", Model.currentPersonID), "--select--")    
       &lt;script>
           function updateFormEnabled() 
           {
               if (verifyAdSettings()) 
               {
                   $('#sbmt').removeAttr('disabled');
               }
               else 
               {
                   $('#sbmt').attr('disabled', 'disabled');
               }
           }

           function verifyAdSettings() 
           {
               if ($('#HolidayDate').val() != '') 
               {
                   return true;
               }
               else 
               {
                   return false;
               }
           }

           $('#HolidayDate').change(updateFormEnabled);

           &lt;/script>

             &lt;input type="submit" id= "sbmt" name="ViewHolidaysDD" value="View"/>
              &lt;/form>
  &lt;script>
      $('#sbmt').attr('disabled', '');
        &lt;/script>

        </div>

</table>

<br />
<br />
<table>
    <div>
    Judging by your selection the order of dates are: //HERE IS WHERE I WANT TO DISPLAY THE ORDER OF DATES
    </div>

</table>

就在上面,我想按顺序显示我的日期,升序

//my create 3 Action result


 [HttpGet]
        public ActionResult Create3()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create3(string date1, string date2, string date3)
        {
            string FirstDateOrder, SecondDateOrder, ThirdDateOrder;

            //date 1 is biggest
            if (date1.Length > date2.Length && date1.Length > date3.Length)
            {
                //date 2 is 2nd & date 3 is 3rd
                if (date2.Length > date3.Length)
                {
                    FirstDateOrder = date1;
                    SecondDateOrder = date2;
                    ThirdDateOrder = date3;

                    System.Windows.Forms.MessageBox.Show("Order is 1, 2, 3");

                    return RedirectToAction("Index");
                }

                //date 3 is 2nd & date 2 is 3rd
                else
                {
                    FirstDateOrder = date1;
                    SecondDateOrder = date3;
                    ThirdDateOrder = date2;

                    System.Windows.Forms.MessageBox.Show("Order is 1, 3, 2");

                    return RedirectToAction("Index");
                }

            }

            //date 2 is biggest
            if (date2.Length > date1.Length && date2.Length > date3.Length)
            {
                //date 1 is 2nd & date 3 is 3rd
                if (date1.Length > date3.Length)
                {
                    FirstDateOrder = date2;
                    SecondDateOrder = date1;
                    ThirdDateOrder = date3;

                    System.Windows.Forms.MessageBox.Show("Order is 2, 1, 3");

                    return RedirectToAction("Index");
                }

                //date 3 is 2nd & date 1 is 3rd
                else
                {
                    FirstDateOrder = date2;
                    SecondDateOrder = date3;
                    ThirdDateOrder = date1;

                    System.Windows.Forms.MessageBox.Show("Order is 2, 3, 1");

                    return RedirectToAction("Index");
                }

            }

            //date 3 is biggest
            if (date3.Length > date1.Length && date3.Length > date2.Length)
            {
                //date 1 is 2nd & date 2 is 3rd
                if (date1.Length > date2.Length)
                {
                    FirstDateOrder = date3;
                    SecondDateOrder = date1;
                    ThirdDateOrder = date2;

                    System.Windows.Forms.MessageBox.Show("Order is 3, 1, 2");

                    return RedirectToAction("Index");
                }

                //date 2 is 2nd & date 1 is 3rd
                else
                {
                    FirstDateOrder = date3;
                    SecondDateOrder = date2;
                    ThirdDateOrder = date1;

                    System.Windows.Forms.MessageBox.Show("Order is 3, 2, 1");

                    return RedirectToAction("Index");
                }

            }


            return RedirectToAction("Index");

}

and my view:

    @model HolidayBookingApp.Models.Dates

@{
    ViewBag.Title = "Create3";
}

<h2>Create3</h2>

&lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">&lt;/script>
&lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript">&lt;/script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Dates</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.date1)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.date1)
            @Html.ValidationMessageFor(model => model.date1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.date2)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.date2)
            @Html.ValidationMessageFor(model => model.date2)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.date3)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.date3)
            @Html.ValidationMessageFor(model => model.date3)
        </div>

        <p>
            &lt;input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>"Index")


</div>

不确定从这里开始的事情就像在索引视图中创建一个参数来完成订单?

任何帮助都会非常感谢大家和对论文的抱歉

1 个答案:

答案 0 :(得分:1)

还有其他更优雅的方法,但最简单的方法是使用ViewBag。而不是:

System.Windows.Forms.MessageBox.Show("Order is 3, 1, 2");

尝试这样的事情:

ViewBag.DateOrder = "Order is 3, 1, 2";

然后在你的视图中简单地说:

<span>@ViewBag.DateOrder</span>