2015-09-29 50 views
1

我正在開發一個項目,我在這裏報告時間。我有一個複選框「沒有時間報告」我檢查,它會禁用所有的字段。因爲如果你沒有東西要報告,你不需要輸入小時等等。Asp.net返回檢查複選框的複選框

但是,在提交我的時間報告後,在返回的視圖上,該複選框將被選中。當我再次回到我的視圖時,我希望它不被檢查。如果我刷新頁面,默認情況下,複選框將被取消選中。

我返回我這樣的看法: return View();我認爲這將是一個刷新?

第一次報告: enter image description here

,但我得到回到同一視圖中的複選框被選中還是: enter image description here

做一個更新修復它,複選框將是錯誤的。但是,必須有一種更簡單的方式才能實現這一功能,而不是刷新頁面?

這是我的控制器:

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> 
+0

您是否嘗試過使用視圖模型? – Canvas

+0

你可以張貼剃刀,特別是渲染複選框的部分嗎? –

+0

@FabioSalvalai完成。更新我的問題。 –

回答

1

請問當你的模型是有效的問題發生呢?

我注意到你實例

projectData = new NewTimeReportModel(); 

,但你從來沒有事後使用它。也許ViewData.Model之前已經設置了另一個過時的實例。

您應該檢查您傳遞的模型是否處於您期望的狀態。

編輯:

此外,

我回到像這樣我的看法:返回查看();我認爲這將與刷新相同?

答案是否定的:它返回一個全新狀態的頁面。你給你的頁面的狀態是你作爲模型傳遞的狀態。要麼作爲View()的參數,要麼通過設置ViewData.Model

+0

我需要我的ViewData.Model那裏或我的視圖將崩潰因對象引用未設置爲對象的實例。 projectData如何保存NoTimeToReport = false,即使我返回它將設置複選框進行檢查。 –

+0

我加載我的設置:LoadDefaultSettings(projectData); NoTimeToReport在此處也設置爲false。然而在返回之後,我的觀點仍然存在。 –

+0

我沒有'LoadDefaultSettings'的細節設置視圖模型嗎?看起來'返回View();'命中,'model.NoTimeToReport'設置爲'true',這就是爲什麼我問。 –

0

我會說你採用的編程風格並不能幫助你清楚地看清事情。

您用於視圖的模型的屬性NoTimeToReport設置爲true而不是false。現在,爲了找到原因,您的代碼會變得很複雜,因爲您的模型不止一次被實例化,其值被​​設置在多個位置,並且該對象不止一次被設置爲模型,使用ViewData.Model = projectData;

相反,推薦的方法是:

  • 實例化模型只有一次
  • 設置的所有屬性,您看合適
  • 決不設置ViewData.Model屬性
  • 傳遞模型到View()方法如下:

    return View(projectData); 
    

這樣一來,你就可以看到哪裏要設置其屬性之一,錯誤值,和你將確保在顯示視圖時使用模型的正確實例。

+0

但即使我將projectTime中的noTimeToReport設置爲false,它仍然會在啓用我的視圖時啓用複選框。 –

+0

是嗎?如果是這樣,也許你仍然在之前的某個地方設置ViewData.Model,並且它優先於提供的模型。 –

+0

看起來像這樣,設置返回視圖NoTimeToReport設置爲True –