我正在開發一個項目,我在這裏報告時間。我有一個複選框「沒有時間報告」我檢查,它會禁用所有的字段。因爲如果你沒有東西要報告,你不需要輸入小時等等。Asp.net返回檢查複選框的複選框
但是,在提交我的時間報告後,在返回的視圖上,該複選框將被選中。當我再次回到我的視圖時,我希望它不被檢查。如果我刷新頁面,默認情況下,複選框將被取消選中。
我返回我這樣的看法: return View();
我認爲這將是一個刷新?
做一個更新修復它,複選框將是錯誤的。但是,必須有一種更簡單的方式才能實現這一功能,而不是刷新頁面?
這是我的控制器:
public ActionResult TimeReport(FormCollection form, Guid? id, bool? noTimeToReport)
{
ShowProjects(true);
NewTimeReportModel projectData = new NewTimeReportModel();
//Deletes Timereport
if (form != null && form.AllKeys.Contains("delete"))
{
new DatabaseLayer().DeleteTimeReport(Guid.Parse(form["ReportId"]));
LoadDefaultSettings(projectData);
ViewData.Model = projectData;
ViewData["deleted"] = true;
return RedirectToAction("Index");
}
//Update Timereport
if (id.HasValue && (form == null || form.AllKeys.Length == 0))
{
using (DatabaseLayer db = new DatabaseLayer())
{
var timeReport = db.GetTimeReport(id.Value);
projectData = new NewTimeReportModel(timeReport);
}
}
//Loads default settings
else if (form == null || form.AllKeys.Length == 0)
{
LoadDefaultSettings(projectData);
}
else
{
//Get's all the dates from the view and formates them to look like yy-mm-dd so we can parse it to a datetime.
List<string> dates = FormateDate(form["date"]);
//Loops over all the dates and saves the dates to the database.
projectData = ReportDates(form, projectData, dates, noTimeToReport);
if (ModelState.IsValid)
{
//If we get this far everything is ok and we save the timereport to the database
projectData.SaveToDatabase(Constants.CurrentUser(User.Identity.Name));
ViewData["posted"] = true;
projectData = new NewTimeReportModel();
}
else if (projectData.Projects.Count == 1)
{
ListAllMssingDays();
ViewData.Model = projectData;
return View(projectData);
}
//Loads default settings if all dates been reported.
LoadDefaultSettings(projectData);
}
//Get's and lists all the missing days
ListAllMssingDays();
return View();
}
這是我的看法:
<div class="col-md-6" id="test12">
<div class="tabbable tabbable-custom tabbable-noborder tabbable-reversed">
<div class="tab-content">
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption">
<span class="caption-subject font-green-sharp bold uppercase">Rapportera tid</span>
</div>
</div>
<div class="portlet-body form">
<div class="form-group">
<label class="col-md-5">Ingen tid att rapportera</label>
@Html.CheckBoxFor(model => model.NoTimeToReport, new { @id = "check" })
</div>
@if (Model.ReportId.HasValue)
{
<div class="form-group">
<label class="col-md-4 control-label">Redigera datum:</label>
<div class="col-md-5">
@Html.TextBox("date", Model.Date.ToShortDateString(), new { @class = "form-control", @readonly = "true" })
</div>
</div>
}
<div class="form-group">
<label class="col-md-4 control-label">Start tid:</label>
<div class="col-md-5">
@Html.TextBox("startTime", Model.Times.StartTime, new { @class = "form-control timepicker timepicker-24" })
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Slut tid:</label>
<div class="col-md-5">
@Html.TextBox("endTime", Model.Times.EndTime, new { @class = "form-control timepicker timepicker-24" })
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Rast Längd:</label>
<div class="col-md-5">
@Html.TextBox("breakTime", Model.Times.BreakTime, new { @class = "form-control timepicker timepicker-24" })
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Tid jobbad:</label>
<div class="col-md-5">
@Html.TextBox("workedHours", Model.Times.WorkedHours, new { @class = "form-control", @disabled = "" })
</div>
</div>
</div>
</div>
</div>
</div>
您是否嘗試過使用視圖模型? – Canvas
你可以張貼剃刀,特別是渲染複選框的部分嗎? –
@FabioSalvalai完成。更新我的問題。 –