当用户进入假期页面时,大家好,他们可以做两件事中的一件
1)使用下拉框选择“人名”并点击“查看”,这将显示此人的所有当前假期
2)点击“创建新”,将用户带到一个创建页面,允许他们添加一个新假期(从这里他们从drop中选择人名,并从日历中选择日期)
这一切都有效,但是如果用户最初遵循选择人名的第一条路径并单击视图(它将显示他们的假期),如果他们然后采用2的路径并单击“创建”它将跳转到创建页。然而,下拉框将返回“选择”,我希望从上一个下拉菜单中选择的现有人员显示在此下拉列表中。
Cookie或网址/参数?
无论如何我卡住了请帮忙
我尝试过一个cookie。
[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();
}
//尝试将index中的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]
一旦我了解了这一点,我怎样才能让我的下拉存储价值? 有什么帮助吗?
由于
答案 0 :(得分:1)
对我来说,cookie是一种在不同页面上存储信息的方式,也是一段时间后用户返回的方式。
我更喜欢使用查询字符串,因为信息需要从一个页面传递到另一个页面。您可以在“创建”按钮单击事件上使用javascript或jquery,查看下拉列表是否有值,将其放入查询字符串并重定向。
我建议阅读下面的内容: http://www.codeproject.com/Articles/43457/Session-Cookie-Query-String-Cache-Variables-Unifie
答案 1 :(得分:1)
尝试使用帮助程序SelectList并传递以查看具有项目列表和所选项目ID的模型。
@Html.DropDownList("name", new SelectList(Model.SomeList, "ItemValueId", "ItemDescription", Model.ItemValueId), new { @class = "someclass" })