1

嘿伙计们,当用户进入假期页面时,他们可以做 2 件事中的 1 件事

1)使用下拉框选择“人名”并单击“查看”,这将显示此人当前的所有假期

2)单击“新建”,这会将用户带到允许他们添加新假期的创建页面(从这里他们从下拉菜单中选择人名并从日历中选择日期)

这一切都有效,但是如果用户最初遵循选择人名并单击视图的第一条路径(它将显示他们的假期),如果他们随后采用路径 2 并单击“创建”,它将跳转到创建页面。但是下拉框将返回“选择”,我希望从上一个下拉列表中选择的现有人员显示在此下拉列表中。

cookie 或 url/参数?

无论如何我卡住了请帮忙

我试过饼干。

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

            if (currentPersonID.HasValue)
            {
                model.currentPersonID = currentPersonID.Value;

            }
            else
            {
                model.currentPersonID = 0;
            }

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

            //hyperlink to sort dates in ascending order
            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();

            var cookie = new HttpCookie("cookie_name", "currentPersonID");
            Response.AppendCookie(cookie);     

            return View(model);
        }

    public ActionResult Create()
        {
            var cookie = Request.Cookies["cookie_name"];
            if (cookie != null)
            {
                string value = cookie.Value;
                //int? value = cookie.Value;
            }

            ViewBag.cookie = cookie.Value;

            ViewBag.Id = new SelectList(db.People, "Id", "Name");
            return View();
        } 

//试图将索引中的 currentPersonID 用作 int 但它不会允许我。

[/code]
My View
[code]
@model HolidayBookingApp.Models.startANDend


@{
    ViewBag.Title = "Create";
}

<p>


  <span>@ViewBag.cookie</span>


<h2>Create</h2>

<form action ="ListHolidays" id="listHolidays" method="post">
@using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
    <fieldset>
        <legend>Holiday</legend>

        <div>
            @Html.LabelFor(model => model.PersonId, "Person")
        </div>

        <div>     
            @Html.DropDownListFor(model => model.PersonId,
                                new SelectList(ViewBag.Id, "Value", "Text"),
                                "---Select---"
                                )   
         @Html.ValidationMessageFor(model => model.PersonId)            
        </div>

        <div>
            @Html.LabelFor(model => model.HolidayDate)
        </div>

        <div>

            @Html.TextBoxFor(model => model.HolidayDate)

            @Html.TextBoxFor(model => model.endDate)
    <script>

//        Date.format = 'dd/m/yyy';
        $("#HolidayDate").addClass('date-pick');
        $("#endDate").addClass('date-pick');
            //$('.date-pick').datePicker//({dateFormat: 'dd-mm-yy'}).val();

//        clickInput: true

        $(function () {
            //3 methods below dont allow user to select weekends
            $('.date-pick').datePicker(
               {
                   createButton: false,
                   renderCallback: function ($td, thisDate, month, year) {
                       if (thisDate.isWeekend()) {
                           $td.addClass('weekend');
                           $td.addClass('disabled');
                       }

                   }
               }
        )

        .bind('click',
            function () {
                $(this).dpDisplay();
                this.blur();
                return false;
            }
        )

        .bind('dateSelected',
            function (e, selectedDate, $td) {
                console.log('You selected ' + selectedDate);
            }
        );

            //        HolidayDate is start date
            $('#HolidayDate').bind('dpClosed',
                    function (e, selectedDates) {
                        var d = selectedDates[0];
                        if (d) {
                            d = new Date(d);
                            $('#endDate').dpSetStartDate(d.addDays(0).asString());
                        }
                    }
            );

            //end date is end date
            $('#endDate').bind('dpClosed',
                    function (e, selectedDates) {
                        var d = selectedDates[0];
                        if (d) {
                            d = new Date(d);
                            $('#HolidayDate').dpSetEndDate(d.addDays(0).asString());
                        }
                    }
                );
        });



    </script>

     @Html.ValidationMessageFor(model => model.HolidayDate)
        </div>

        <p>
            <input type="submit" value="Create"/>
        </p>



    </fieldset>
}

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

@*
<p>Current Person Selected is:

@TempData["currentPersonID"]

</p>*@
[code]

一旦我开始这样做,我怎样才能让我的下拉菜单存储价值?有什么帮助吗?

谢谢

4

2 回答 2

1

对我来说,cookie 是一种跨许多不同页面存储信息的方式,如果用户在一段时间后返回,它也是一种方式。

我更喜欢使用查询字符串,因为信息需要从一页传递到另一页。您可以在“创建”按钮单击事件上使用 javascript 或 jquery,查看下拉菜单是否有值,将其放入查询字符串并重定向。

我建议阅读以下内容:http: //www.codeproject.com/Articles/43457/Session-Cookie-Query-String-Cache-Variables-Unifie

于 2012-12-12T15:41:48.807 回答
1

尝试使用帮助器 SelectList 并传递以查看带有项目列表和所选项目 id 的模型。

@Html.DropDownList("name", new SelectList(Model.SomeList, "ItemValueId", "ItemDescription", Model.ItemValueId), new { @class = "someclass" })
于 2012-12-12T15:43:36.590 回答