2012-05-10 22 views
-2

誰能告訴我如何從以下範圍內調用該函數GetDuration:如何從這個特定的上下文中調用我的javascript功能

@{ 
... 
... 
<div> GetDuration(@item.StartDate, @item.EndDate) </div> 
... 
... 
} 

功能GetDuration()動態創建DIV提到父內的另一個DIV在我的背景下。

我有2個問題:

  • 我不知道如何從代碼中顯示的當場打電話給我的功能GetDuration()
  • startdatum.Month.ToString("MMMM");回報"MMMM"代替月份名稱

提前致謝!

這是我的代碼:

<script type="text/javascript"> 

function GetDuration(startdatum, einddatum) { 
    var maand1 = startdatum.Month.ToString("MMMM"); 
    var maand2 = einddatum.Month.ToString("MMMM"); 
    var duration = "Date: "; 
    if (maand1 == maand2) { 
     duration += item.Startdate.Day.ToString() 
      + " - " + item.Enddate.Day.ToString() 
      + " " + maand1 
      + " " + item.Startdate.Year.ToString(); 
    } 
    else { 
     duration += item.Startdate.Day.ToString() 
      + item.Startdate.Month.ToString("MMMM") 
      + " - " + item.Enddate.Day.ToString() 
      + " " + item.Enddate.Month.ToString("MMMM") 
      + " " + item.Startdate.Year.ToString(); 
    } 
    var dynDiv = document.createElement("div"); 
    dynDiv.class = "HP_DateDiv"; 
    dynDiv.innerHTML = duration; 
    document.body.appendChild(dynDiv); 
} 

</script> 


    @{ string prevYear = ""; 

    foreach (var item in ViewData.Model) { 

     string year = item.StartDate.Year.ToString(); 
     if (year != prevYear) 
     { 
      <div class="contentheader"> 
       @year Formula 1 Tickets 
      </div> 

      prevYear = year; 
     } 
     <div class="HP_eventBodyDiv"> 
      <div class="HP_picDiv" style = "width:100px; height:100px;"> 
       <a href="http://www.cssdrive.com/" class="highlightit"><img src="../../Content/Pics/auto.jpg" alt="oto" height="50" width="50"/></a> 
      </div> 
      <div class="HP_eventDiv"> 
       <div class="HP_eventNameDiv">@item.Name 
      </div> 
<!--This is the spot to call my function with something like "GetDuration(@item.StartDate, @item.EndDate)"--> 
       <div class="HP_viewBuyBtnsDiv"> 
        <input id="btn_View" type="button" value="View Event" class="contentbtn"/> 
        <input id="btn_Buy" type="button" value="Buy Tickets" class="contentbtn"/> 
       </div> 
      </div> 
     </div>    

     }; 
    } 
+1

我缺少的東西? 'GetDuration'看起來不像JavaScript,它在'text/javascript'腳本塊中。 – Tejs

+2

什麼是@ {...}'? –

+4

歡迎來到[SO]。請花一些時間閱讀[常見問題]。如果你花一些額外的時間來確保你的文章格式良好並且可以很容易閱讀,那麼通常會很感激。 – zzzzBov

回答

0

我要看看我是否能解釋這一點給你。 Razor語法允許我們做一些很棒的事情,比如你對你的看法。在視圖中迭代集合的能力非常好,並且可以爲創建動態頁面提供一些很好的靈活性。 他們的服務器實際上是使用您提供的語法創建Razor視圖,然後將構建HTML頁面發送給客戶端。這包括腳本標籤。所以你試圖做的並不是真正的工作。你不能在@foreach中迭代調用JavaScript函數就像那樣,因爲頁面還沒有被渲染。 一旦Razor View完全形成,您應該只考慮使用Javascript。然後你可以使用JavaScript來編輯DOM。

所以我的建議是編輯說。讓服務器計算該屬性並將其與模型一起發送。

編輯:看着更新的代碼後

等待我認爲你要對這個錯誤的方式。我不明白你爲什麼使用JavaScript來計算你的持續時間。你應該編輯你正在通過的模型來包含持續時間,特別是如果它是這樣的計算字段。那麼您可以像其他任何屬性一樣顯示持續時間。

編輯2:

public class MyNewClass 
{ 
public DateTime StartDate {get;set;} 
public DateTime EndDate {get;set;} 
public int Duration 
{ 
get{ 
//Do your Logic Here with startdate and enddate 
} 
set; 
} 
} 
+0

對不起,我不知道你的意思。在我的視圖的頂部已經有一個腳本標記,但結束標記位於中間的某處,並且我繼續使用@ {...... CODE ....} – Ger

+0

是的,那是可能性,但我認爲他會更好。我似乎在不斷更新我的所有Web服務以滿足我的需求,而我卻故意嘗試保留我的web服務。我從來沒有使用過jQuery。這有可能嗎?爲什麼我可以從例如onclick事件調用我的函數,但不能從DIV內容中調用? – Ger

+0

好,非常感謝您的時間。我會改變我的模型。 – Ger

相關問題